public void doLoop() throws IOException {
        int nEvents;
        processRegistrationQueue();

        while (dontStop) {
            nEvents = selector.select(timeout);
            if (nEvents > 0) {
                for (Iterator<SelectionKey> i = selector.selectedKeys()
                        .iterator(); i.hasNext();) {
                    SelectionKey sk = i.next();
                    i.remove();

                    if (!sk.isValid())
                        continue;

                    Object arg = sk.attachment();
                    callback.handleEvent(sk, arg);
                }
            }

            if (this.registrationQueue.size() > 0)
                processRegistrationQueue();

            if (registrationRequests > 0) {
                synchronized (registrationLock) {
                    while (registrationRequests > 0) {
                        try {
                            registrationLock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }