It's important to recognize the importance of working in flow state. Four half-hour chunks of time isn't equivalent to having one continuous two hour block of time because it takes time to remember where I left off.
To find continuous blocks of time to code / write, I've started saying 'no' to some social invitations, even when I think the events would be fun. For example, if a group of friends is watching a movie that I've already seen, I'll politely say 'no' and use that time to work because it isn't really hanging out and I'm not working.
I try to avoid multitasking when I'm out with friends, but to keep my mind focused, I need to know that I'll have time to tinker later. I do that by saying 'no.'
Compartmentalizing is key. Make the time you spend count.