Replacing the charging port should do the trick. I've repaired lots of iphones with this kind of issues and it leads to the charging port. Try cleaning first your headset port with a slim cotton swab and a little drop of alcohol on the swab. If it doesn't work replacing it will be your last resort.