I generally experience this when I'm not holding onto my consciousness long enough to slip into a dream. What I do to keep that balance between sleep and awake is a little meditation trick I learned when visiting a monastery. Count from one to ten, one count for every breath you inhale. When you get to ten, go back to one. It takes very little focus so you can fall asleep, but making sure you don't count on to eleven, twelve... makes sure you don't black out completely.