#!/usr/bin/perl -w
use strict;
use IO::Socket;

sub connect_client {
    my ( $remote, $port );
    my ( $iaddr, $paddr, $proto, $line );
    
    $remote  = shift || 'localhost';
    $port    = shift || 2342;  # random port

    my $conn_hand = IO::Socket::INET->new(
				    Proto    => "tcp",
				    PeerAddr => $remote,
				    PeerPort => $port
				       )
	or die "ERROR: Cannot connect to port $port at $remote\n";

    $conn_hand->autoflush(1);
    print $conn_hand "get termsize\n";
    # send( SOCK, "get termsize\n", 0);
    my ( $cols, $rows ) = ( 0, 0 );
    if (defined(my $line = <$conn_hand>)) {
	print $line;
	if ( $line =~ /^cols:\s(\d*?),\srows:\s(\d*?)$/ ) {
	    $cols = $1;
	    $rows = $2;
	    print "termsize: ok $cols $rows\n";
	    return(0, $conn_hand, $cols, $rows);
	} else {
	    print "termsize: error";
	    return(1);
	}
    }
}


sub disconnect_client{
    close ($_[0])            || die "close: $!";
}

my ( $retval, $conn_hand, $cols, $rows ) = connect_client();
if ( $retval != 0 ) {
    die "ERROR: Couldn't establish a connection.\n";
}
disconnect_client( $conn_hand );
exit;

