#!/usr/bin/perl -w
use strict;

use Socket;
use IO::Handle;

my($remote,$port,$iaddr,$paddr,$proto,$line,$input);

$remote = shift || 'localhost';
$port = shift || 25;		# SMTP port



if($port =~/\D/) {$port = getservbyname($port, 'tcp')}
die "No port" unless $port;
$iaddr=inet_aton($remote);
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
connect(SOCK, $paddr) || die "connect: $!";
SOCK->autoflush(1);

sub readLines {
    while(defined($line = <SOCK>)) {
	print $line;
    }
}

while($input = <>) {
    print "Input: '$input'\n";
    print SOCK $input;
    &readLines;
} 

close(SOCK) || die "close: $!";
exit;



# Local Variables: ***
# mode: Perl ***
# End: ***
