title: prevent "No buffer space available" error in ruby
body: <p>I have a small script which scans all the ips ranging from 192.168.190.xxx to 192.168.220.xxx on port 411.</p>
  
  <p>The script works fine sometimes, but sometimes I get the error "No buffer space available"
  <code>dcport.rb:8:in</code>initialize': No buffer space available - connect(2) (Errno::ENOBUFS)`</p>
  
  <p>I have read that this occurs when the socket were not closed properly, but I have used <code>mysocket.close</code> to prevent that which I suppose does not work properly.</p>
  
  <p>How to prevent this from happening, I mean how to close the socket properly?</p>
  
  <p>My code is as follows</p>
  
  <pre><code>require 'socket'
  require 'timeout'
  (190...216).each do |i|
    (0...255).each do |j|
      begin
        #puts "Scanning 192.168.#{i}.#{j}"
        scan=Timeout::timeout(10/1000.0) {
          s=TCPSocket.new("192.168.#{i}.#{j}",411)
          s.close
          puts "192.168.#{i}.#{j} =&gt; Hub running"
        }
      rescue Timeout::Error
      rescue Errno::ENETUNREACH
      rescue Errno::ECONNREFUSED
      end
    end
  end
  </code></pre>