title: How to handle socket disconnects in Dart?
body: <p>I'm using Dart 1.8.5 on server.
  I want to implement TCP Socket Server that listens to incoming connections, sends some data to every client and stops to generate data when client disconnects.</p>
  
  <p>Here is the sample code</p>
  
  
  
  <pre class="lang-dart prettyprint-override"><code>void main() {
    ServerSocket.bind(
        InternetAddress.ANY_IP_V4,
        9000).then((ServerSocket server) {
      runZoned(() {
        server.listen(handleClient);
      }, onError: (e) {
        print('Server error: $e');
      });
    });
  }
  
  void handleClient(Socket client) {
    client.done.then((_) {
      print('Stop sending');
    });
    print('Send data');
  }
  </code></pre>
  
  <p>This code accepts connections and prints "Send data". But it will never print "Stop sending" even if client was gone.</p>
  
  <p>The question is: how to catch client disconnect in listener?</p>