/
ColdCore-2.1/
#!/usr/bin/perl

($them, $port) = @ARGV;
$port = 1138 unless $port;
$them = 'localhost' unless $them;

$logging = 0;

$SIG{'INT'} = 'dokill';
sub dokill { kill 9,$child if $child; }

require 'sys/socket.ph';

$sockaddr = 'S n a4 x8';
chop($hostname = `hostname`);

($name, $aliases, $proto) = getprotobyname('tcp');
($name, $aliases, $port) = getservbyname($port, 'tcp')
    unless $port =~ /^\d+$/;
($name, $aliases, $type, $len, $thisaddr) = gethostbyname($hostname);
($name, $aliases, $type, $len, $thataddr) = gethostbyname($them);

$this = pack($sockaddr, &AF_INET, 0, $thisaddr);
$that = pack($sockaddr, &AF_INET, $port, $thataddr);

socket(S, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!";
bind(S, $this) || die "bind: $!";
connect(S, $that) || die "connect: $!";

select(S); $| = 1;

if ($child = fork) {
    print S "guest dumpguest d@c.u\n";
    while (<>) {
        print S;
    }
    sleep 1;
    do dokill();
} else {
    select(stdout); $| = 1; 
    open(LOG, ">logfile");
    $lines = 0;
    while (<S>) {
        if ($logging) {
            if (/^#\$#\/LOG/) {
                close(LOG);
                $logging = 0;
                print STDOUT "\n";
            } else {
                print LOG;
                $lines++;
                !($lines % 500) && print ".";
            }
        } else {
            if (/^#\$#LOG/) {
                $logging = 1;
                print STDOUT "Logging to file";
            } else {
                print STDOUT;
            }
        }
    }
    close(LOG);
}