     (cond (#{:stdout :stderr} type)
           (update-context id type conj content)
           (#{:result :error :read-error} type)
-          (update-context id :results conj [type content]))
-    (prn data)))
+          (let [count (update-context id :result inc)]
+            #_(prn @contexts)
+            ((get-in (context id) [:callbacks count])
+             (let [vals (butlast (.split content "\n"))]
+               (if (= type :result)
+                 vals
+                 (conj (butlast vals) {type (last vals)}))))))
+    #_(prn data)))
 
 (defn connect
   ([port] (connect port "localhost"))
   ([port host]
-     (let [sock (.createConnection net port host)]
-       (.on sock "data" on-data))))
+     (doto (.createConnection net port host)
+       (.on "data" on-data))))
+
+(defn test-fn [data]
+  (prn "result" data))
 
 (defn -main [& args]
   (let [sock (connect 1337 "localhost")]
-    (eval sock "(range 1000)")
-    (eval sock "(+ 5 5)")))
+    (eval sock "(+ 1 2 3 4 5)" test-fn)
+    (eval sock "(slurp \"http://lazybot.org\")" test-fn)))
 
 (set! *main-cli-fn* -main)
(defn parse-message
  "Parse a Portal message."
  [message]
  (let [message (second (.split (apply str (butlast (str message)))":"))
        [id type content] (.split message " ")]
    {:type (keyword type)
     :id id
     :content content}))

(defn send-message
  "Send a portal message."

(defn context
  "Look up a context. If no context exists, create one."
  [id]
  (let [id (str id)]
    (or (@contexts id)
        (get
         (swap! contexts assoc id
                {:results {}
                 :eval 0
                 :result 0
                 :callbacks {}
                 :stdout []
                 :stderr []})
         id))))

(defn update-context
  "Update a context."
  [id key f & args]
  (let [id (str id)]
    (context id)
    (get-in
     (swap! contexts update-in [id key] (fn [old] (apply f old args)))
     [id key])))

(defn netstring
  "Create a netstring."
