title: Bridging standard input/output over a TCP socket
body: <p>I want to write a script that does the following:</p>
  
  <ol>
  <li>Start listening on a random available localhost TCP port.</li>
  <li>Start a certain external program passing the port number as an argument.</li>
  <li>Accept a single connection to the server socket.</li>
  <li>Send the script’s standard input into the socket, and the socket’s output to standard output.</li>
  <li>Exit when the external program exits.</li>
  </ol>
  
  <p>The ideal solution would be a shell script invoking some reasonably standard tools, so if anybody can come up with a way to do that, well, that would be the perfect answer. If that proves intractable, a Ruby implementation would be convenient for me. Failing that, I’ll take any workable implementation.</p>
  
  <p>P.S. I’m pretty new here, so please let me know if this question is too general or if it doesn’t belong for some other reason (maybe "please implement X for me" is frowned upon?).</p>