@@ -139,17 +139,17 @@ class RealtimeClient {
139139        (String  payload, Function (dynamic  result) callback) => 
140140            callback (json.decode (payload));
141141    reconnectTimer =  RetryTimer (
142-       () {
143-         disconnect ();
144-         connect ();
142+       () async   {
143+         await   disconnect ();
144+         await   connect ();
145145      },
146146      this .reconnectAfterMs,
147147    );
148148  }
149149
150150  /// Connects the socket. 
151151   @internal 
152-   void  connect () async  {
152+   Future < void >  connect () async  {
153153    if  (conn !=  null ) {
154154      return ;
155155    }
@@ -161,8 +161,15 @@ class RealtimeClient {
161161      try  {
162162        await  conn! .ready;
163163      } catch  (error) {
164-         _onConnError (error);
165-         reconnectTimer.scheduleTimeout ();
164+         // Don't schedule a reconnect and emit error if connection has been 
165+         // closed by the user or [disconnect] waits for the connection to be 
166+         // ready before closing it. 
167+         if  (connState !=  SocketStates .disconnected && 
168+             connState !=  SocketStates .disconnecting) {
169+           connState =  SocketStates .closed;
170+           _onConnError (error);
171+           reconnectTimer.scheduleTimeout ();
172+         }
166173        return ;
167174      }
168175
@@ -176,7 +183,8 @@ class RealtimeClient {
176183        onError:  _onConnError,
177184        onDone:  () {
178185          // communication has been closed 
179-           if  (connState !=  SocketStates .disconnected) {
186+           if  (connState !=  SocketStates .disconnected && 
187+               connState !=  SocketStates .disconnecting) {
180188            connState =  SocketStates .closed;
181189          }
182190          _onConnClose ();
@@ -189,17 +197,26 @@ class RealtimeClient {
189197  }
190198
191199  /// Disconnects the socket with status [code]  and [reason]  for the disconnect 
192-    void  disconnect ({int ?  code, String ?  reason}) {
200+    Future < void >  disconnect ({int ?  code, String ?  reason})  async  {
193201    final  conn =  this .conn;
194202    if  (conn !=  null ) {
195-       final  connectionWasOpen =  connState ==  SocketStates .open;
196-       connState =  SocketStates .disconnected;
197-       if  (connectionWasOpen) {
203+       final  oldState =  connState;
204+       connState =  SocketStates .disconnecting;
205+ 
206+       // Connection cannot be closed while it's still connecting. Wait for connection to 
207+       // be ready and then close it. 
208+       if  (oldState ==  SocketStates .connecting) {
209+         await  conn.ready.catchError ((_) {});
210+       }
211+ 
212+       if  (oldState ==  SocketStates .open || 
213+           oldState ==  SocketStates .connecting) {
198214        if  (code !=  null ) {
199-           conn.sink.close (code, reason ??  '' );
215+           await   conn.sink.close (code, reason ??  '' );
200216        } else  {
201-           conn.sink.close ();
217+           await   conn.sink.close ();
202218        }
219+         connState =  SocketStates .disconnected;
203220        reconnectTimer.reset ();
204221      }
205222      this .conn =  null ;
@@ -264,8 +281,8 @@ class RealtimeClient {
264281        return  'connecting' ;
265282      case  SocketStates .open: 
266283        return  'open' ;
267-       case  SocketStates .closing : 
268-         return  'closing ' ;
284+       case  SocketStates .disconnecting : 
285+         return  'disconnecting ' ;
269286      case  SocketStates .disconnected: 
270287        return  'disconnected' ;
271288      case  SocketStates .closed: 
@@ -275,7 +292,7 @@ class RealtimeClient {
275292  }
276293
277294  /// Retuns `true`  is the connection is open. 
278-    bool  get  isConnected =>  connectionState  ==  ' open'  ;
295+    bool  get  isConnected =>  connState  ==  SocketStates . open;
279296
280297  /// Removes a subscription from the socket. 
281298   @internal 
@@ -374,7 +391,7 @@ class RealtimeClient {
374391    }
375392  }
376393
377-   /// Unsubscribe from channels with the specified topic. 
394+   /// Unsubscribe from joined or joining  channels with the specified topic. 
378395   @internal 
379396  void  leaveOpenTopic (String  topic) {
380397    final  dupChannel =  channels.firstWhereOrNull (
0 commit comments