Low beam works when pulled out of headlight socket but when put in it goes out?

2002 Honda accord strange occurrence and can't figure it out. The one low beam was out so I went to replace the bulb I twisted the plug out of the headlight itself and the low beam came on stayed on for a while then went out put it back into the headlight, pulled it out again and the same thing I tried this a few times and every time I pull the plug and bulb out the bulb lights up?

The bulb socket may need to be replaced.

There's a short in your head light casing

Try another bulb and see if it still goes out. That way you'll know if it's just the broken element in the old bulb that touches and makes contact. If it still goes out inspect the bulb connector. You'll either have a broken wire or a loose contact in the harness plug for the bulb.