require 'socket'
require 'sanford-protocol'

module AndSon

  class Connection < Struct.new(:host, :port)
    module NoRequest
      def self.to_s; "[?]"; end
    end

    def open
      protocol_connection = Sanford::Protocol::Connection.new(tcp_socket)
      yield protocol_connection if block_given?
    ensure
      protocol_connection.close
    end

    private

    # TCP_NODELAY is set to disable buffering. In the case of Sanford
    # communication, we have all the information we need to send up front and
    # are closing the connection, so it doesn't need to buffer.
    # See http://linux.die.net/man/7/tcp

    def tcp_socket
      TCPSocket.new(host, port).tap do |socket|
        socket.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, true)
      end
    end

  end

end

require 'socket'
require 'sanford-protocol'

module AndSon

  class Connection < Struct.new(:host, :port)
    module NoRequest
      def self.to_s; "[?]"; end
    end

    def open
      protocol_connection = Sanford::Protocol::Connection.new(tcp_socket)
      yield protocol_connection if block_given?
    ensure
      protocol_connection.close if protocol_connection
    end

    private

    # TCP_NODELAY is set to disable buffering. In the case of Sanford
    # communication, we have all the information we need to send up front and
    # are closing the connection, so it doesn't need to buffer.
    # See http://linux.die.net/man/7/tcp

    def tcp_socket
      TCPSocket.new(host, port).tap do |socket|
        socket.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, true)
      end
    end

  end

end

Stop confusing exception when AndSon can't bind to a server
Stop confusing exception when AndSon can't bind to a server

Previously, we were getting an `NoMethodError` for `close` on the
connection (because it could never make a connection) when a
client couldn't bind. This fixes that by making sure protocol
connection has been set before trying to close it. With this, you
will get an exception like: `Errno::ECONNREFUSED`, which is
the normal exception for the problem.

Fixes #13
