private int onReadyToWrite(long now) throws IOException {
        this.selector.checkThread();
        this.lastEventTime = now;

        int sent = 0;

        // copy outbound bytes into network buffer
        ByteBuffer buffer = this.selector.networkBuffer;
        buffer.clear();
        this.outbound.get(buffer);
        buffer.flip();

        // write to channel
        if (buffer.hasRemaining()) {
            SocketChannel channel = (SocketChannel) key.channel();
            sent = channel.write(buffer);
        }

        if (sent > 0) {
            this.outbound.discard(sent);
        }

        for (PyroClientListener listener: this.listeners)
            listener.sentData(this, sent);

        this.adjustWriteOp();

        if (this.doShutdown && !this.outbound.hasData()) {
            this.dropConnection();
        }

        return sent;
    }