The first thing to try in cases like this is a reset of the phone. To do this, hold the power and home button together until the phone turns off. Once that happens, release both buttons and let the phone start up again. Once the phone is started up, see if it's still happening.
If you are still having issues, the problem likely lies with the 30-pin connector. While you can try cleaning it with 91% isopropyl alcohol, the replacements are incredibly cheap, but can be cleaned by letting them soak in alcohol while scrubbing the gold contacts on the connector to clean them up. If you just want to replace the connector, the part can be found here. The guide can be found here.
if that does not work, the phone has a motherboard problem. At this point, it's usually cheaper to pick up another 4S used or purchase a newer model like the 5s for a little more money then you would pay for a 4s.