#!/usr/bin/perl 
require 5.002;
use strict;
BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
use Socket qw(SOCK_STREAM getaddrinfo getnameinfo NI_NUMERICHOST NIx_NOSERV  );

use Carp;
use IO::Select;

sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }

my $host = shift || in6addr_any();
my $port = shift || 2345;
my $proto = getprotobyname('tcp');

use IO::Socket;
use Socket qw(SOCK_STREAM getaddrinfo);

my %hints = (socktype => SOCK_STREAM);
my ($err, @res) = getaddrinfo( $host  , $port , \%hints);
die "Cannot getaddrinfo - $err" if $err;

my $sock;

foreach my $ai (@res) {
      my ($err, $ipaddr) = getnameinfo($ai->{addr}, NI_NUMERICHOST, NIx_NOSERV);
      my ($line);


      # $proto   = getprotobyname('tcp');

        my ($socket) = IO::Socket->new();
        $socket->socket($ai->{family}, $ai->{socktype}, $ai->{protocol});
        $socket->connect( $ai->{addr})    || next ;

        syswrite $socket, $ENV{'USER'}."\n";

        while (defined($line = <$socket>)) {
         print $line;
        }

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

}

