There is such a thing as “process addiction” (as opposed to substance addiction). Process addictions including things like gambling, shopping and many would say internet/information-based activity. In general, addicts substitute meaningful activity that is both challenging and rewarding with “meaningless” activity that simply provides feelings similar to a reward. So, rather than sweating to climb a mountain one step at a time, they will opt for a summit-like feeling. Oftentimes, this is because the stress created by the thought of climbing the mountain is beyond their immediate ability to cope. Addiction frequently happens in a cycle so in addition addicts will also “use” or “act out” to cope further with the disappointment of each lost opportunity.
Possibly, when you fall into the routine you describe you are creating feelings of accomplishment or some kind of high or trance state. Other people are better at managing and balancing their play time online with all of the other things they must do or want to do because they find those other activities meaningful. From what you describe, it sounds like you might have trouble maintaining that balance.
One approach is to practice mindfulness—that is, try as much as possible to maintain an awareness of your focus. Be observant of your intention, the point when your mind wanders, and when you’ve strayed from your intended activity. Straying is part of the process, so expect it to happen and refrain from being critical of yourself. If you catch yourself wandering, acknowledge that you’ve wandered and then return to your intended activity.
The hard part about writing and many other meaningful activities is the inertia required to get in to the activity. If you are like me, sitting down to write or to do something else that is challenging quickly leads to a panicky feeling and that opens the floodgates for mass distraction. You have to learn to be patient and calm, sit out that panicky feeling and trust a) that something will come if you sit quietly for long enough and b) that the panicky feeling will diminish with time and effort. For starters, though, just do your best to be observant of the panicky feeling (or whatever your feeling happens to be). Don’t try like crazy to assuage it like a crying baby (or girlfriend). Just watch it, and let it cry itself out.
You should expect to repeat this process many times and frequently—maybe as frequently as every ten minutes. Over time, though, it should subside.
@Qingu has a good point about ritual… I would venture to guess that you have a ritual already in place that encompasses your distractions. That’s another area to observe and change.