title: How to handle connection status of sockets in Flutter?
body: <p>I am building an app that requires to connect to a videoconference device in order to send call commands.</p>
  <p>My problem comes when the device gets disconnected because it's powered off, cable disconnection, or any other issue. In that scenario, I am able to show it in the app thanks to the <code>vcfConnected</code> variable. However, I can never get the connection back to work (only by restarting the app, which is not desirable)</p>
  <p>I am connecting to the device via dart socket like follows. The idea behind this implementation is to try to reconnect whenever there is a problem. However, is not working fine.</p>
  <p>Does anybody know how to better handle the connection state?</p>
  <p>Is it better to connect to the device each time I have to send a command and close the socket after that each time?</p>
  <pre><code>void connect(){
    Socket.connect('192.168.5.107', 55003).then((Socket socket) {
      sock = socket;
      vcfConnected = true;
      socket.listen((data) {
        String response = hex.encode(data);
        dataHandler(context, response);
      },
          onError: (error) =&gt; errorHandler(error),
          onDone: () =&gt; reconnect()
      );
      // Init protocol message
      sendCommand('&amp;IPV');
  
      getMicMuteState();
      getReceiveVolume();
  
      timer = Timer.periodic(Duration(seconds: 10), (Timer t) =&gt; checkVcfStatus());
    }).timeout(Duration(seconds: 15), onTimeout: () =&gt; onTimeout());
  
  }
  
  FutureOr&lt;Null&gt; onTimeout(){
    reconnect();
  }
  
  void reconnect(){
    print('VCF disconnected');
    vcfConnected = false;
    if (timer != null){
      timer.cancel();
    }
    if (sock != null) {
      sock.close();
    }
    sock = null;
    connect();
  }
  
  void errorHandler(, error){
    print('Error: $error');
    reconnect();
  }
  
  void checkVcfStatus() {
    sendCommand('?S');
  }
  </code></pre>