@Override
    public void run() {
        try {
            // Check if we know if it is a server or client
            if (socket.getInputStream().read() == 1) {
                connectedToServer = true;
                logger.debug("Start handling new connection from: " + socket.getInetAddress().getHostAddress() + " (servermode)");
            } else {
                connectedToServer = false;
                logger.debug("Start handling new connection from: " + socket.getInetAddress().getHostAddress() + " (clientmode)");
            }

            // Handle stuff            
            byte[] buffer = new byte[8];
            int length;

            // Read the requests
            while ((length = socket.getInputStream().read(buffer)) != -1) {
                String requestString = new String(buffer, 0, length).trim();

                switch (requestString) {
                    case ConnectionMessages.SERVER_FILE_PULL:
                        handleFilePullRequest();
                        break;
                    case ConnectionMessages.SERVER_FILE_PUSH:
                        handleFilePushRequest();
                        break;
                    case ConnectionMessages.SERVER_FILE_DELETE:
                        handleFileDeleteRequest();
                        break;

                    case ConnectionMessages.SERVER_META_DOWNLOAD:
                        handleMetaDownload();
                        break;
                    case ConnectionMessages.SERVER_META_UPDATE:
                        handleMetaUpdate();
                        break;

                    default:
                        logger.error("Unknown request '" + requestString + "', closing connection.. ");
                        break;

                }
            }
            logger.debug("Handling finished, closing connection...");

        } catch (Exception ex) {
            // Special exception catcher, just to be sure
            logger.error("Handling the client connection went wrong. Outer exception handler reached!", ex);
        } finally {
            try {
                socket.close();
            } catch (IOException ex) {
                // ignore
            }
            parent.notifyDisconnect(this);
        }
    }