# File lib/socksify.rb, line 222
  def socks_connect(host, port)
    port = Socket.getservbyname(port) if port.is_a?(String)
    req = String.new
    Socksify::debug_debug "Sending destination address"
    req << TCPSocket.socks_version
    Socksify::debug_debug TCPSocket.socks_version.unpack "H*"
    req << "\001"
    req << "\000" if @@socks_version == "5"
    req << [port].pack('n') if @@socks_version =~ /^4/

    if @@socks_version == "4"
      host = Resolv::DNS.new.getaddress(host).to_s
    end
    Socksify::debug_debug host
    if host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/  # to IPv4 address
      req << "\001" if @@socks_version == "5"
      _ip = [$1.to_i,
             $2.to_i,
             $3.to_i,
             $4.to_i
            ].pack('CCCC')
      req << _ip
    elsif host =~ /^[:0-9a-f]+$/  # to IPv6 address
      raise "TCP/IPv6 over SOCKS is not yet supported (inet_pton missing in Ruby & not supported by Tor"
      req << "\004"
    else                          # to hostname
      if @@socks_version == "5"
        req << "\003" + [host.size].pack('C') + host
      else
        req << "\000\000\000\001"
        req << "\007\000"
        Socksify::debug_notice host
        req << host
        req << "\000"
      end
    end
    req << [port].pack('n') if @@socks_version == "5"
    write req

    socks_receive_reply
    Socksify::debug_notice "Connected to #{host}:#{port} over SOCKS"
  end