I'm afraid the only way is to make the accepting program act as a proxy between the other program and the remote machine.
So basically, it'd look like this:
- Program A: incoming connection
- Program A accepts and does whatever needs to be done
- Program A finds that it is now Program B's responsibilty. Either 1) using the existing TCP connection between A and B, or 2) by establishing a new connection, A informs B about this.
- Program A maintains the connection to the host but doesn't do anything except for bidirectionally relaying all data it receives from either party.
- When program B is done as well, it tells A about it, either by sending a specific code over the existing connection or simply closing the one created in step 3.2.
- Program A performs the goodbye rituals as per protocol (such as sending a 'everything succesful' sort of status message) and closes the connection to the remote as well.
I could be mistaken, of course, but to my knowledge this is the only way to fulfill your requirements. It'd be much simpler, of course, if Program A could simply say "Hey, listen, I can't do anything for you anymore, but here, have this state information (session ID or just the session data, something like that, so that Program B will know what it's working with) and contact my friend, program B. You can find him on this-and-that address."