@Override
    public void run() {

        try {
            startServer();
        } catch (IOException e) {
            e.printStackTrace();
        }

        while (!this.isInterrupted()) {

            try {
                selector.select();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Iterator keys = selector.selectedKeys().iterator();

            while (keys.hasNext()) {
                SelectionKey key = (SelectionKey) keys.next();

                //prevent the same key from coming up again
                keys.remove();


                if (!key.isValid()) {
                    continue;
                }

                if (key.isAcceptable()) {
                    acceptOP(key, selector);
                } else if (key.isReadable()) {
                    this.readOP(key);
                } else if (key.isWritable()) {
                    this.writeOP(key);
                }

            }
        }
    }