Sounds like, if you’ll excuse the pun, a connection internally from a board to the speaker and headphone socket. As it changes when you flex the phone that clearly indicates a damaged connection, possibly a dry solder joint or a joint damaged by the phone being slightly bent in use, like being compressed in a pocket, sitting on it accidentally, or something like that. It might be fixable, try taking it into an Apple store and speaking to one of the Genius people. Make sure you back it up fully before hand, it’s not unknown for them to just replace the handset. Do you have replacement insurance from your network provider? They might replace it for you, the 3Gs is now being given away free on some networks.