Very difficult to say without seeing exactly what you are doing.
If it is a strain to hold yourself in the bent leg position you may not be quite vertical and will not be able to balance. Once there is some ease in this position and you feel balanced, safe, connected and able to breathe fully slowly then you are ready to slowly bring your legs up.
Ask your teacher to watch you and let you know what you are doing wrong causing the loss of balance.
If you do not have a teacher try videoing yourself (from the side) and check that your hips/butt is directly over your head and your back is vertical.....watch yourself and notice what you do/do not do just before you fall. Your neck and back should be in a natural position.....just the same as standing....the natural curves of the spine should be there.....
You can use the wall partly to stop yourself falling and to find the sensations of being fully vertical and balanced. If you come into headstand one shin length away from the wall then place one foot on the wall with knee at 90 other leg straight up with knees together.....that should be vertical.....again ask some one to look or use video.
You could also try slowly lifting both knees up away from your chest....keep legs bent heels near your butt......slowly bring knees up over hips....then slow straighten legs. (this works best away from the wall)
It is a good idea to practice headstand with a teacher who can guide you both verbally and with hands on help.
If there is any pain or tension in your neck please stop immediately.