+ local $^W;
+ require POSIX; POSIX->import(qw(O_NONBLOCK F_SETFL F_GETFL))
+ };
+
+ eval {
+ local $^W;
+ require IO::Socket::INET6;
+ };
+
+ if ($@) {
+ dbg($@);
+ require IO::Socket;
+ $io_socket = 'IO::Socket::INET';
+ } else {
+ $io_socket = 'IO::Socket::INET6';
+ }
+ $io_socket->import;
+
+ if ($@ || $main::is_win) {
+ $blocking_supported = $io_socket->can('blocking') ? 2 : 0;
+ } else {
+ $blocking_supported = $io_socket->can('blocking') ? 2 : 1;
+ }
+
+
+ # import as many of these errno values as are available
+ eval {
+ local $^W;
+ require Errno; Errno->import(qw(EAGAIN EINPROGRESS EWOULDBLOCK));
+ };
+
+ unless ($^O eq 'MSWin32') {
+ if ($] >= 5.6) {
+ eval {
+ local $^W;
+ require Socket; Socket->import(qw(IPPROTO_TCP TCP_NODELAY));
+ };
+ } else {
+ dbg("IPPROTO_TCP and TCP_NODELAY manually defined");
+ eval 'sub IPPROTO_TCP { 6 };';
+ eval 'sub TCP_NODELAY { 1 };';
+ }
+ }
+ # http://support.microsoft.com/support/kb/articles/Q150/5/37.asp
+ # defines EINPROGRESS as 10035. We provide it here because some
+ # Win32 users report POSIX::EINPROGRESS is not vendor-supported.
+ if ($^O eq 'MSWin32') {
+ eval '*EINPROGRESS = sub { 10036 };' unless defined *EINPROGRESS;
+ eval '*EWOULDBLOCK = *EAGAIN = sub { 10035 };' unless defined *EWOULDBLOCK;
+ eval '*F_GETFL = sub { 0 };';
+ eval '*F_SETFL = sub { 0 };';
+ eval '*IPPROTO_TCP = sub { 6 };';
+ eval '*TCP_NODELAY = sub { 1 };';
+ $blocking_supported = 0; # it appears that this DOESN'T work :-(
+ }
+}
+
+my $w = $^W;
+$^W = 0;
+my $eagain = eval {EAGAIN()};
+my $einprogress = eval {EINPROGRESS()};
+my $ewouldblock = eval {EWOULDBLOCK()};
+$^W = $w;
+$cnum = 0;
+
+
+#
+#-----------------------------------------------------------------
+# Generalised initializer
+
+sub new
+{
+ my ($pkg, $rproc) = @_;
+ my $obj = ref($pkg);
+ my $class = $obj || $pkg;
+
+ my $conn = {
+ rproc => $rproc,
+ inqueue => [],
+ outqueue => [],
+ state => 0,
+ lineend => "\r\n",
+ csort => 'telnet',
+ timeval => 60,
+ blocking => 0,
+ cnum => (($cnum < 999) ? (++$cnum) : ($cnum = 1)),