def connect
    site = @proxy || @dest
    begin
      retry_number = 0
      timeout(@connect_timeout) do
        @socket = create_socket(site)
        begin
          @src.host = @socket.addr[3]
          @src.port = @socket.addr[1]
        rescue SocketError
          # to avoid IPSocket#addr problem on Mac OS X 10.3 + ruby-1.8.1.
          # cf. [ruby-talk:84909], [ruby-talk:95827]
        end
        if @dest.scheme == 'https'
          @socket = create_ssl_socket(@socket)
          connect_ssl_proxy(@socket) if @proxy
          @socket.ssl_connect
          @socket.post_connection_check(@dest)
          @ssl_peer_cert = @socket.peer_cert
        end
        # Use Ruby internal buffering instead of passing data immediatly
        # to the underlying layer
        # => we need to to call explicitely flush on the socket
        @socket.sync = @socket_sync
      end
    rescue TimeoutError
      if @connect_retry == 0
        retry
      else
        retry_number += 1
        retry if retry_number < @connect_retry
      end
      close
      raise
    end

    @state = :WAIT
    @readbuf = ''
  end
def connect
    site = @proxy || @dest
    begin
      retry_number = 0
      timeout(@connect_timeout) do
        @socket = create_socket(site)
        begin
          @src.host = @socket.addr[3]
          @src.port = @socket.addr[1]
        rescue SocketError
          # to avoid IPSocket#addr problem on Mac OS X 10.3 + ruby-1.8.1.
          # cf. [ruby-talk:84909], [ruby-talk:95827]
        end
        if @dest.scheme == 'https'
          @socket = create_ssl_socket(@socket)
          connect_ssl_proxy(@socket) if @proxy
          @socket.ssl_connect
          @socket.post_connection_check(@dest)
          @ssl_peer_cert = @socket.peer_cert
        end
        # Use Ruby internal buffering instead of passing data immediatly
        # to the underlying layer
        # => we need to to call explicitely flush on the socket
        @socket.sync = @socket_sync
      end
    rescue TimeoutError
      if @connect_retry == 0
        retry
      else
        retry_number += 1
        retry if retry_number < @connect_retry
      end
      close
      raise
    end

    @state = :WAIT
    @readbuf = ''
  end
Connect to the server