Can you describe exactly what you're doing? Do you have an alloy or carbon steerer?
If the headset is coming loose it means that the stem is able to move up the steerer, suggesting the clamp bolts aren't tight enough. This will also pull the top cap upwards, probably moving the star nut or expander inside the steerer.
Procedure should be:
loosen the 2 stem clamp bolts
gently tighten the top cap bolt until there's no play or binding in the headset. This bolt needs a low torque; it's sole purpose in life is to load the bearings properly.
Ensuring the steerer is pointing in the right direction, progressively tighten the stem clamp bolts. Use a torque wrench if you have a carbon steerer.
If you cannot remove the play, take off the top cap and check that the top of the steerer tube sits at least 3 mm below the top of the stem or the upper spacer.