       case Erlang.sets.is_element(conn, seen_already) do
       match: :true
         ExLog.info "Skipping #{inspect(conn)}"
-        query_peers command, data, seen_already
+        query_peers command, data, seen_already, conn_lookup
       match: :false
         ExLog.info "Connecting to #{inspect(conn)}"
-        {:ok, socket} = Erlang.gen_tcp.connect domain, port, [:binary, {:active, :true}]        
+        {:ok, socket} = Erlang.gen_tcp.connect domain, port, [:binary, {:active, :true}]      
         Erlang.gen_tcp.send socket, command
-        query_peers command, data, Erlang.sets.add_element({domain, port}, seen_already)
+        query_peers command, data, Erlang.sets.add_element(conn, seen_already), Erlang.dict.store(socket, conn, conn_lookup)
       end
-    match: {:tcp, _port, new_data}
+    match: {:tcp, socket, new_data}
       # Received new data from a peer, append it to the list
-      ExLog.info "received new data: #{inspect(new_data)}"
-      query_peers command, [new_data | data], seen_already
+      {domain, port} = Erlang.dict.fetch(socket, conn_lookup)
+      conn = [list_to_binary(domain), list_to_binary(integer_to_list(port))]
+      ExLog.info "received new data: #{inspect(new_data)} from #{inspect(conn)}"
+      query_peers command, [[conn, new_data] | data], seen_already, conn_lookup
     match: {:tcp_closed, _port}
       # A peer closed the socket, probably ok
-      query_peers command, data, seen_already
+      query_peers command, data, seen_already, conn_lookup
     after: __MODULE__.__info__(:data)[:browse_timeout]
       # No messages after a few milliseconds, assume that everyone has 
       # reported in and return the data. 

  def query_peers(command) do
    ExLog.info "querying peers for #{command}"
    Erlang.dnssd.browse(__MODULE__.__info__(:data)[:service_type])
    query_peers command, [], Erlang.sets.new()
  end

  defp query_peers(command, data, seen_already) do
    receive do
    match: {:dnssd, _ref, {:browse, :add, response={name, type, domain}}}
      # Found a peer, now get the port that it's listening on
      Erlang.dnssd.resolve name, type, domain
      query_peers command, data, seen_already
    match: {:dnssd, _ref, {:resolve, response={domain_dot, port, _dns_data}}}
      # Resolved the port of a peer, now connect to it and send the query
      domain = binary_to_list(Regex.replace(%r/\.$/, domain_dot, ""))

    timeout = 10000
    case transport.recv(socket, 0, timeout) do
    match: {:ok, data}
      handle_request(data, socket, transport)
    match: {:error, reason}
      ExLog.info "Error: #{inspect reason}"
      transport.close(socket)
    end
  end

  # Request handling logic

  def handle_request(request="check_data", socket, transport) do
    response = inspect({CerebrateChecks.get_all()})
    ExLog.info "received data: #{request}, responding with #{response}"
    case transport.send(socket, response) do
    match: :ok
      :ok
    match: {:error, reason}
      ExLog.info "Send error: #{inspect(reason)}"
    end
  end

  def handle_request(unknown_request, socket, transport) do
    ExLog.info "Unknown request: #{unknown_request}"
    transport.close(socket)
  end

end


