diff --git a/src/common/xmpp/client_nb.py b/src/common/xmpp/client_nb.py
index 0e372dd2e0af878b1bc0753fbc4bb8779d485d63..def945d4f64378a334e10491b55164c2a6a32a5a 100644
--- a/src/common/xmpp/client_nb.py
+++ b/src/common/xmpp/client_nb.py
@@ -306,10 +306,18 @@ class NonBlockingClient:
 			if data:
 				self.Dispatcher.ProcessNonBlocking(data)
 			if not hasattr(self, 'Dispatcher') or \
-			self.Dispatcher.Stream._document_attrs is None:
+				self.Dispatcher.Stream._document_attrs is None:
 				self._xmpp_connect_machine(
 					mode='FAILURE',
 					data='Error on stream open')
+				return
+
+			# if terminating stanza was received after init request then client gets
+			# disconnected from bosh transport plugin and we have to end the stream
+			# negotiating process straight away.
+			# fixes #4657
+			if not self.connected: return
+
 			if self.incoming_stream_version() == '1.0':
 				if not self.got_features:
 					on_next_receive('RECEIVE_STREAM_FEATURES')