   proxied using a SOCKS proxy."
   [^String hostname ^Integer port]
   (let [socket-factory #(socks-proxied-socket hostname port)
-        reg (doto (SchemeRegistry.)
-              (.register
-               (Scheme. "https" 443 (SSLGenericSocketFactory socket-factory)))
-              (.register
-               (Scheme. "http" 80 (PlainGenericSocketFactory socket-factory))))]
-    (PoolingClientConnectionManager. reg)))
+        reg (-> (RegistryBuilder/create)
+                (.register "http" (PlainGenericSocketFactory socket-factory))
+                (.register "https" (SSLGenericSocketFactory socket-factory))
+                (.build))]
+    (PoolingHttpClientConnectionManager. reg)))
 
 (def insecure-scheme-registry
   (-> (RegistryBuilder/create)

(def ^SSLConnectionSocketFactory secure-ssl-socket-factory
  (SSLConnectionSocketFactory/getSocketFactory))

;; New Generic Socket Factories that can support socks proxy
(defn ^SSLSocketFactory SSLGenericSocketFactory
  "Given a function that returns a new socket, create an SSLSocketFactory that
  will use that socket."
  [socket-factory]
  (proxy [SSLSocketFactory] [(SSLContexts/createDefault)]
    (connectSocket [socket remoteAddress localAddress params]
      (let [^SSLSocketFactory this this] ;; avoid reflection
        (proxy-super connectSocket (socket-factory)
                     remoteAddress localAddress params)))))

(defn ^PlainSocketFactory PlainGenericSocketFactory
  "Given a Function that returns a new socket, create a PlainSocketFactory that
  will use that socket."
  [socket-factory]
  (proxy [PlainSocketFactory] []
    (createSocket [params]
      (socket-factory))))

(defn socks-proxied-socket

            [clojure.java.io :as io])
  (:import (java.net Socket Proxy Proxy$Type InetSocketAddress)
           (java.security KeyStore)
           (org.apache.http.config RegistryBuilder Registry)
           (org.apache.http.conn HttpClientConnectionManager)
           (org.apache.http.conn.ssl DefaultHostnameVerifier
                                     NoopHostnameVerifier
                                     SSLConnectionSocketFactory
                                     SSLContexts
                                     TrustStrategy)
           (org.apache.http.conn.socket PlainConnectionSocketFactory)
           (org.apache.http.impl.conn BasicHttpClientConnectionManager
                                      PoolingHttpClientConnectionManager)))

(def ^SSLConnectionSocketFactory insecure-socket-factory
  (SSLConnectionSocketFactory.
