@Override
    public void completed(Object result, Object attachment) {
        while (buffer.position() < buffer.capacity()) {
            socket.write(buffer, null, this);
        }
        buffer.clear();
        try {
            socket.close();
        }
        catch (IOException exc) {
            exc.printStackTrace();
        }
    }