 
 (declare boot)
 
+(on ::recv-osc-message (fn [msg]
+                         (dosync (ref-set recv-queue* (assoc @recv-queue* msg)))
+                         (println (str msg))))
+
+(on ::send-osc-message (fn [msg]
+                         (let [buffer (java.nio.ByteBuffer/allocate 8129)]
+                           (osc-encode-msg buffer msg)
+                           (.flip buffer)
+                           (World_SendPacket @world* (.limit buffer) buffer
+                                             (callback reply-cb
+                                                       (fn [addr buf size]
+                                                         (event ::recv-osc-message
+                                                                (osc-decode-packet (.getByteBuffer buf 0 size)))))))))
+
+(on ::send-osc-message-noreply (fn [msg]
+                                 (let [buffer (java.nio.ByteBuffer/allocate 8129)]
+                                   (osc-encode-msg buffer msg)
+                                   (.flip buffer)
+                                   (World_SendPacket @world* (.limit buffer) buffer nil))))
+
+(defn snd-nr
+  "Creates an OSC message and dont expect a reply back."
+  [path & args]
+    (cond 
+      (= ::external (type @server*)) (osc-send-msg @server* 
+                                                   (apply osc-msg path (osc-type-tag args) args))
+      (= ::internal (type @server*)) (event ::send-osc-message (apply osc-msg path (osc-type-tag args) args))))
+
 (defn snd
   "Creates an OSC message and either sends it to the server immediately
   or if a bundle is currently being formed it adds it to the list of messages."

  "Block with a timeout to receive a message on a given osc path from
  the server."
  [path & [timeout]]
  (osc-recv @server* path timeout))

; Notifications from Server
; These messages are sent as notification of some event to all clients who have registered via the /notify command .


(def N-RETRIES 50)

(defn- connect-thread-external
  [host port]
   (log/debug "Connecting to external SuperCollider server: " host ":" port)
   (dosync (ref-set server* (with-meta
                              (osc-client host port)
                              {:type ::external})))
