private void waitForConnections() {

        while (true) {
            try {
                acceptConnection();
            } catch (SocketTimeoutException e) {
                if (stopWaiting) {
                    try {
                        serverSocket.close();
                    } catch (IOException e1) {
                        listener.onConnectionFailed(null, port, e1);
                    }
                    return;
                }
            } catch (IOException e) {
                listener.onConnectionFailed(null, port, e);
            }
        }
    }