#!/usr/bin/perl
#
#
use IO::Socket::INET;

my $hostname = shift;
my $port     = shift;

if (!$port) {
    print "usage: $0 <hostname> <port>\n";
    exit 1;
}

$| = 1;     # auto-flush

my $socket = new IO::Socket::INET (
    PeerHost => $hostname,
    PeerPort => $port,
    Proto => 'tcp',
) or die "Cannot connect to $hostname:$port: $!\n";

while (my $input = <>) {
    if (!$socket->send($input)) {
        die "Cannot send data to $hostname:$port: $!\n";
    }
}
shutdown($socket, 1);
my $response = '';
$socket->recv($response, 1024);

my $cnt=1;
select(undef,undef,undef,0.1);
while ($socket->connected) {
    print "Wait for shutdown of Benno Archive " if $cnt == 1;
    select(undef,undef,undef,fibonacci($cnt));
    print '.';
    $cnt++ >= 11 && last;
}
print " $response";
$socket->close();

### subs ###
sub fibonacci {
    my $n = shift;
    $n < 3 ? 1 : fibonacci($n-1) + fibonacci($n-2)
}
