public synchronized boolean dispatch() throws IOException {
		
		try {
			while (this.messages.isEmpty()) {
				wait();
			} 
		} catch (InterruptedException ex) {
			//The thread was interrupted, silent failure.
			System.out.println("Thread interrupted");
			return false;
		}
		
		Message msg = this.messages.remove(0);
		JSONObject send = new JSONObject();
		try {
			send.put("type",    msg.getType());
			
			if (msg.getPayload() instanceof String) {
				send.put("payload", msg.getPayload());
			} else {
				JSONObject payload = (JSONObject)msg.getPayload();
				send.put("action", payload.get("action"));
				send.put("args",   payload.get("args"));
			}
			
			if (msg.getSrc() != null) {
				send.put("src",     msg.getSrc().getClientName());
			}
		} catch (JSONException e) {}
		this.socket.write(send.toString());
		
		return true;
		
	}