@Override
    public void deliver(Packet packet) throws UnauthorizedException {
        if (isClosed()) {
            if (backupDeliverer != null) {
                backupDeliverer.deliver(packet);
            } else {
                Log.trace("Discarding packet that was due to be delivered on closed connection {}, for which no backup deliverer was configured.", this);
            }
        }
        else {
            boolean errorDelivering = false;
            try {
                channelHandlerContext.writeAndFlush(packet.getElement().asXML())
                    .addListener(l ->
                        updateWrittenBytesCounter(channelHandlerContext)
                    );
                // TODO - handle errors more specifically
                // Currently errors are handled by the default exceptionCaught method (log error, close channel)
                // We can add a new listener to the ChannelFuture f for more specific error handling.
            }
            catch (Exception e) {
                Log.debug("Error delivering packet:\n" + packet, e);
                errorDelivering = true;
            }
            if (errorDelivering) {
                close();
                // Retry sending the packet again. Most probably if the packet is a
                // Message it will be stored offline
                if (backupDeliverer != null) {
                    backupDeliverer.deliver(packet);
                } else {
                    Log.trace("Discarding packet that failed to be delivered to connection {}, for which no backup deliverer was configured.", this);
                }
            }
            else {
                session.incrementServerPacketCount();
            }
        }
    }
Flag that specifies if the connection should be considered closed. Closing a NIO connection
is an asynchronous operation so instead of waiting for the connection to be actually closed just
keep this flag to avoid using the connection between #close was used and the socket is actually
closed.
xmppserver/src/main/java/org/jivesoftware/openfire/nio/NettyConnection.java