# File lib/excon/connection.rb, line 19
    def initialize(url, params = {})
      uri = URI.parse(url)
      @connection = Excon.defaults.merge({
        :host       => uri.host,
        :host_port  => '' << uri.host << ':' << uri.port.to_s,
        :path       => uri.path,
        :port       => uri.port.to_s,
        :query      => uri.query,
        :scheme     => uri.scheme,
      }).merge!(params)
      # merge does not deep-dup, so make sure headers is not the original
      @connection[:headers] = @connection[:headers].dup

      @proxy = nil

      if @connection[:scheme] == HTTPS && (ENV.has_key?('https_proxy') || ENV.has_key?('HTTPS_PROXY'))
        @proxy = setup_proxy(ENV['https_proxy'] || ENV['HTTPS_PROXY'])
      elsif (ENV.has_key?('http_proxy') || ENV.has_key?('HTTP_PROXY'))
        @proxy = setup_proxy(ENV['http_proxy'] || ENV['HTTP_PROXY'])
      elsif @connection.has_key?(:proxy)
        @proxy = setup_proxy(@connection[:proxy])
      end

      if @proxy
        @connection[:headers]['Proxy-Connection'] ||= 'Keep-Alive'
        # https credentials happen in handshake
        if @connection[:scheme] == 'http' && (@proxy[:user] || @proxy[:password])
          auth = ['' << @proxy[:user].to_s << ':' << @proxy[:password].to_s].pack('m').delete(Excon::CR_NL)
          @connection[:headers]['Proxy-Authorization'] = 'Basic ' << auth
        end
      end

      if ENV.has_key?('EXCON_DEBUG') || ENV.has_key?('EXCON_STANDARD_INSTRUMENTOR')
        @connection[:instrumentor] = Excon::StandardInstrumentor
      end

      # Use Basic Auth if url contains a login
      if uri.user || uri.password
        @connection[:headers]['Authorization'] ||= 'Basic ' << ['' << uri.user.to_s << ':' << uri.password.to_s].pack('m').delete(Excon::CR_NL)
      end

      @socket_key = '' << @connection[:host_port]
      reset
    end