#!/usr/local/bin/ruby
require 'tserver'
if ARGV.length != 3 then
  puts "Usage: port-forward <local-port> <remote-host> <remote-port>"
  exit
end
localport, remotehost, remoteport = ARGV
ts = TServer::new(localport)
ts.start do |s|
  s2 = TCPSocket::new(remotehost, remoteport)
  begin
    loop do
      rfds, = IO::select([s,s2])
      rfds.each do |r|
	w = r == s ? s2 : s
	w.syswrite r.sysread(1024)
      end
    end
  rescue EOFError, Errno::ECONNRESET
  end
  s2.close
end
