@Nonnull
    private Optional<HttpConnection> getConnectionReadyForOutboundDelivery()
    {
        synchronized (connectionQueue) {
            // The connection queue is ordered. No need to iterate further than the first element.
            if (!connectionQueue.isEmpty()) {
                final HttpConnection connection = connectionQueue.peek();
                assert !connection.isClosed();

                // We can only use connections that have a sequential request ID.
                if (connection.getRequestId() <= lastSequentialRequestID) {
                    Log.trace("Got a connection that is ready for outbound delivery of session {}. The connection's RID is {}. The last sequential RID was: {}", getStreamID(), connection.getRequestId(), lastSequentialRequestID);
                    connectionQueue.poll(); // remove it.
                    return Optional.of(connection);
                } else {
                    Log.trace("Trying to get a connection that is ready for outbound delivery of session {}, but the first connection in the connection queue isn't the next connection that needs to be responded to. It's RID is {}, while the last sequential RID was {}.", getStreamID(), connection.getRequestId(), lastSequentialRequestID);
                }
            } else {
                Log.trace("Trying to get a connection that is ready for outbound delivery of session {}, but the connection queue is currently empty. The last sequential RID was {}", getStreamID(), lastSequentialRequestID);
            }
        }
        return Optional.empty();
    }
Returns a connection that can be used to deliver data to the client, if such a connection is currently available.

@return A connection that is ready to be responded to.
xmppserver/src/main/java/org/jivesoftware/openfire/http/HttpSession.java