public void run() {

        boolean connectionFailed = false;
        while (running) {
            try {
                RS232CmdData cmdData = null;
                if (queue == null) {
                    logger.warning("Waiting for queue to reset");
                    Thread.sleep(1000);
                }
                cmdData = queue.take();
                if (cmdData != null) {
                    byte code = cmdData.getCode();
                    byte[] arg = cmdData.getArgument();
                    byte[] cmd = null;
                    if (arg != null) {
                        cmd = new byte[arg.length + 1];
                        cmd[0] = code;
                        for (int i = 1; i < cmd.length; i++) {
                            cmd[i] = arg[i - 1];
                        }
                    } else {
                        cmd = new byte[1];
                        cmd[0] = code;
                    }
                    logger.info("Sending: --" + Utils.byteArrayToHexString(cmd)
                            + "--");
                    outputStream.write(cmd);
                }
            } catch (InterruptedException ie) {
                // ignore
            } catch (Exception e) {
                logger.log(Level.WARNING, "Exception sending cmd: ", e);
                logger.log(Level.WARNING, "Caused by: " + e.getCause());
                connectionFailed = true;
            }
            
            if (connectionFailed) {
                logger.warning("Retrying socket: " + remoteIpAddress + ":" + remotePortNumber);
                try {
                    close();
                    setupSocket();
                    connectionFailed = false;
                } catch (Exception e1) {
                    logger.log(Level.WARNING, "Unable to reconnect socket", e1);
                }

            }
        }
    }