My phone is having the same issue. I've rooted and installed many different ROMs. The last one I tried called Beanstalk installed with all the sound working properly but on the first reboot, the problem came back. I tried wiping an re-installing but the problem is still there, so it seems to be the luck of the draw. There is an app called Soundabout which you can install to route the sound to the speakers, but you would then have to go into the app to route the sound back to the headphones when you need. Very strange. I've seen many posts on different boards of people with the same issue but no true fix. I need to find a good android developer who knows the code well that could probably figure this out...but who that man could be I just don't know...
Are you still facing the problem?