@Override
    protected Void doInBackground() throws Exception {

        //InetAddress addr=InetAddress.getLocalHost();
        
        Socket sock=null;
        ServerSocket serverSock=new ServerSocket(PORT);
        try {
            while (true) {
                sock=serverSock.accept();

                sockIn=sock.getInputStream();
                sockOut=sock.getOutputStream();

                byte[] buf=new byte[4096];
                int rd=sockIn.read(buf,0,buf.length);

                if (rd > 0) {
                    Command scratchCommand=new Command();
                    scratchCommand.scratchRaw=new String(Arrays.copyOf(buf, rd));

                    if (scratchCommand.scratchRaw.toLowerCase().contains("crossdomain.xml")) {
                        sendPolicyFile();
                    }
                    else {
                        commandHandler.signalScratchMessageReception(); // Indirect GUI update
                        
                        if (scratchCommand.scratchRaw.contains("/poll")) {
                            // Poll command to check against status of blocking commands status map
                            // -> not implemented: commandHandler.checkBlockingCommandStatus(scratchCommand);
                            // Will return a status object with status value and potential error message to Scratch
                            sendResponse("OK");
                        }
                        else {
                            // Operative command - forward to NAO client for handling
                            sendResponse("OK");
                            if (NaoClient.getInstance().isConnected()) {
                                commandHandler.queueQ(scratchCommand);
                            }
                        }
                        
                        /* Using SwingWorker's "publish(scratchCommand.scratchRaw)" would be
                        the most clean solution. However, changing into the EDT scope
                        takes so much time that we cannot keep up with Scratch's 30 
                        commands per second. For this reason we use basic inter-
                        thread communication while staying in the worker thread's 
                        context. */
                    }
                }
                try { sock.close(); } catch(Exception e) {}
            } // while(true)
        }
        catch(Exception ex) {
            ex.printStackTrace(System.err);
        }
        finally {
            try { sock.close(); } catch(Exception e) {}
            try { serverSock.close(); } catch(Exception e) {}
        }
        return null;
    }