private void requestWriting() throws Exception {
        for (;;) {
            if (writing.compareAndSet(false, true)) {
                // We are now in writing mode and only we can write to the socket
                return;
            }
            else {
                // Check health of the socket
                if (checkHealth()) {
                    // Connection was closed then stop
                    throw new Exception("Probable dead connection was closed");
                }
                else {
                    Thread.sleep(1);
                }
            }
        }
    }
Returns true if the socket was closed due to a bad health. The socket is considered to
be in a bad state if a thread has been writing for a while and the write operation has
not finished in a long time or when the client has not sent a heartbeat for a long time.
In any of both cases the socket will be closed.

@return true if the socket was closed due to a bad health.
xmppserver/src/main/java/org/jivesoftware/openfire/net/SocketConnection.java