@Override
    public void run() {
        while (!socket.isClosed()) {
            DatagramPacket p = new DatagramPacket(new byte[10], 10);
            try {
                socket.receive(p);
            }
            catch (IOException e) {
                continue;
            }

            Message m = new Message(p.getData());
            if (m.getMessageType() == MessageType.CONFIRMED_REQUEST) {
                System.out.println("Received confirmed request: id=" + m.getMessageId() + ", value=" + m.getValue());

                // Wait for a sec before sending response
                try {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e1) {
                    // no op
                }

                Message response = new Message(MessageType.RESPONSE, m.getMessageId(), (byte) (m.getValue() + 1));
                try {
                    send(response, p.getAddress(), p.getPort());
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else if (m.getMessageType() == MessageType.UNCONFIRMED_REQUEST)
                System.out.println("Received unconfirmed request: id=" + m.getMessageId() + ", value=" + m.getValue());
            else if (m.getMessageType() == MessageType.RESPONSE) {
                System.out.println("Received response: id=" + m.getMessageId() + ", value=" + m.getValue());
                synchronized (waitingRoom) {
                    WaitingRoomMember wrm = waitingRoom.get(m.getMessageId());
                    if (wrm == null)
                        System.out.println("No waiter for response id: " + m.getMessageId());
                    else
                        wrm.setResponse(m);
                }
            }
        }
        System.out.println("Message control ended: port=" + port);
    }