My jabber server (https://prosody.im/) refuses to speak with my phpBB-3.1.5 installation unless I add a usleep(1000); to the sending part: link
After some investigation, I found the following;
The problem we have now, is that we don't wait for the jabber server to acknowledge the stream closing, but rather shutdown the socket immediately. If the jabber server did not send the message yet, it will get discarded as the client connection has been terminated.
What should happen is to wait for the server to respond with </stream:stream> before shutting down.
My dirty solution is to put a $this->listen(); here. This could lead to problems if phpbb receives a random message as it doesnt validate thats its actually a stream closing event.