title: Perl nonblocking socket
body: <p>I want to use nonblocking socket connection, but can't find any examples for understand th main idea.
  I need client what will not block program execution when it will connect to server.
  Now I have the following code:</p>
  
  <pre><code>use IO::Socket;
  use IO::Select;
  use strict;
  $|=1;
  my $host="10.0.0.12";
  my $SELECT = new IO::Select;
  print "Connecting...";
  my $sock=new IO::Socket::INET (
      PeerAddr =&gt; $host,
      PeerPort =&gt; 3128,
      Proto =&gt; 'tcp',
      Blocking =&gt; 0);
      if(!$sock)
      {
          print "Could not create socket: $!n";
      }
      #print "ok\n";
      $SELECT-&gt;add($sock);
      my $buf;
      while (1){
      if($sock and $sock-&gt;connected())
      {
          print "ok\n";
      }
      while (my @ready=$SELECT-&gt;can_read(0.5))
      {
          foreach my $child (@ready)
          {
              if(!sysread($child, $buf, 256))
              {
                  $SELECT-&gt;remove($child);
                  next;
              }
  
          }
      }
      sleep 1;
  }
  </code></pre>
  
  <p>When socket connects $sock->connected() return true and I can do something. But how can check socket for timeout? If it can't connect and closed by timeout I can't check that! How can I do it?</p>
  
  <p>Added:
  Oh, I see!
  Piece of code</p>
  
  <pre><code>if(!sysread($child, $buf, 256))
  {
    $SELECT-&gt;remove($child);
    next;
  }
  </code></pre>
  
  <p>closes socket when timeout is expired!</p>