   "Forwards events to Graphite."
   (:refer-clojure :exclude [replace])
   (:import
-   (java.net Socket)
+   (java.net Socket
+             DatagramSocket
+             DatagramPacket
+             InetAddress)
    (java.io Writer
             OutputStreamWriter)
    (org.jboss.netty.channel Channels)

  [opts]
  (let [opts (merge {:host "127.0.0.1"
                     :port 2003
                     :path graphite-path-percentiles} opts)
        pool (fixed-pool
               (fn open []
                 (info "Connecting to " (select-keys opts [:host :port]))
                 (let [sock (Socket. (:host opts) (:port opts))
                       out  (OutputStreamWriter. (.getOutputStream sock))]
                   (info "Connected")
                   [sock out]))
               (fn close [[sock out]]
                 (info "Closing connection to "
                       (select-keys opts [:host :port]))
                 (.close out)
                 (.close sock))
               {:size                 (:pool-size opts)
                :block-start          (:block-start opts)
                :regenerate-interval  (:reconnect-interval opts)})


    (fn [event]
      (when (:metric event)
        (with-pool [client pool (:claim-timeout opts)]
                   (let [string (str (join " " [(path event)
                                                (float (:metric event))
                                                (int (:time event))])
                                     "\n")]
                     (prn "sending " client string)
                     (send! client string)))))))
