I have an idea what it could be, its possible the socket that plugs into the headlight has corroded from a bad connection, and the plastic is melting with the lights on.
The fact that it lights up when you move the wires points to a bad connection, its called "a wiggle test".
Go through the motions of replacing the headlamp bulb and look into the socket to see if it looks damaged. Hope this helps, John