Twitching and kicking before sleep. Why do I only do it sometimes?

Probably benign. Most normal people have had "sleep starts", a sudden jerk of the body, when falling asleep, or when aroused. This is worse when sleep deprived. Repeated movements of the legs is periodic leg movements of sleep, a variation of restless legs syndrome. This can make the legs and feet twitch hundreds of times in the night; it can be easily treated by prescription medication.