The first one was programming; I'd been working on a little image editing application, implementing anti-aliased gradient brushes (one colour in the centre and another at the edges, with a fast enough and close enough hack to deal with the jagged edges).
The fever had me hallucinating circles and how to render them for the entire (unbelievably tedious) night.
The other time I'd been playing RA95 for a few hours (I was addicted to that shit) and started feeling progressively worse over the space of a few minutes.
I called it an early night (it was about eight o'clock) and went to bed - then it got worse;
The fever, the headache, and an army of little men and tanks running all over the ceiling.
Drifting to sleep and waking up with that book in my head was a ride.
I find that doing something else for about an hour before going to sleep helps with this almost 100% of the time. It could be listening to music, watching TV, reading, playing with my kid, almost anything. But giving my head a full hour to rest from a deep problem helps me sleep without ending up in some logical nightmare that only some construct from my problem could resolve (and tends to make worse)