             [clojure.java.io :as io])
   (:import (java.net Socket Proxy Proxy$Type InetSocketAddress)
            (java.security KeyStore)
-           (java.security.cert X509Certificate)
-           (javax.net.ssl SSLSession SSLSocket)
            (org.apache.http.config RegistryBuilder Registry)
            (org.apache.http.conn HttpClientConnectionManager)
-           (org.apache.http.conn.ssl SSLSocketFactory)
-           (org.apache.http.conn.scheme PlainSocketFactory
-                                        SchemeRegistry Scheme)
+           (org.apache.http.conn.ssl DefaultHostnameVerifier
+                                     NoopHostnameVerifier
+                                     SSLConnectionSocketFactory
+                                     SSLContexts
+                                     TrustStrategy)
            (org.apache.http.conn.socket PlainConnectionSocketFactory)
-           (org.apache.http.impl.conn PoolingClientConnectionManager)
            (org.apache.http.impl.conn BasicHttpClientConnectionManager
-                                      PoolingHttpClientConnectionManager)
-           (org.apache.http.conn.ssl SSLConnectionSocketFactory
-                                     DefaultHostnameVerifier
-                                     NoopHostnameVerifier
-                                     TrustStrategy)
-           (org.apache.http.ssl SSLContexts)
-           (org.apache.http.pool ConnPoolControl)))
+                                      PoolingHttpClientConnectionManager)))
 
 (def ^SSLConnectionSocketFactory insecure-socket-factory
   (SSLConnectionSocketFactory.

(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

  proxied using a SOCKS proxy."
  [^String hostname ^Integer port]
  (let [socket-factory #(socks-proxied-socket hostname port)
        reg (-> (RegistryBuilder/create)
                (.register "http" (PlainGenericSocketFactory socket-factory))
                (.register "https" (SSLGenericSocketFactory socket-factory))
                (.build))]
    (PoolingHttpClientConnectionManager. reg)))

(def insecure-scheme-registry
  (-> (RegistryBuilder/create)
