title: I always get errors in closing handshake of Websockets in Python
body: <p>I've coded a little server and client with Python3, Asyncio and Websockets.</p>
  
  <p>I generate just for testing random numbers from a array, parse them into json and send them as websocket to the server. But often I get many errors because of closing handshakes.</p>
  
  <p>Here is the code:</p>
  
  <p>Server:</p>
  
  <pre><code>import asyncio
  import websockets
  import json
  
  
  async def receiver(websocket, path):
      ws = await websocket.recv()
      print("&lt; {}".format(ws))
  
  
  start_server = websockets.serve(receiver, 'localhost', 8765)
  
  asyncio.get_event_loop().run_until_complete(start_server)
  asyncio.get_event_loop().run_forever()
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>import asyncio
  import websockets
  import json
  from array import *
  import random
  
  
  async def randomNumbers():
      while True:
          numbers = array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9])
          random.shuffle(numbers)
          await sendWebsocket(numbers[0])
          await asyncio.sleep(1)
  
  
  async def sendWebsocket(number):
      async with websockets.connect('ws://localhost:8765') as websocket:
          ws = json.dumps({"number": number})
          await websocket.send(ws)
          print("&gt; {}".format(ws))
  
  
  loop = asyncio.get_event_loop()
  try:
      asyncio.ensure_future(randomNumbers())
      loop.run_forever()
  finally:
      print("Client1 closed")
      loop.close()
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Error in closing handshake
  Traceback (most recent call last):
    File "C:\Users\dgred\AppData\Local\Programs\Python\Python36-32\lib\site-packages\websockets\server.py", line 145, in handler
      yield from self.close()
    File "C:\Users\dgred\AppData\Local\Programs\Python\Python36-32\lib\site-packages\websockets\protocol.py", line 370, in close
      self.timeout, loop=self.loop)
    File "C:\Users\dgred\AppData\Local\Programs\Python\Python36-32\lib\asyncio\tasks.py", line 358, in wait_for
      return fut.result()
    File "C:\Users\dgred\AppData\Local\Programs\Python\Python36-32\lib\site-packages\websockets\protocol.py", line 642, in write_frame
      "in the {} state".format(self.state.name))
  websockets.exceptions.InvalidState: Cannot write to a WebSocket in the CLOSING state
  </code></pre>
  
  <p>Can anyone help me here?</p>
  
  <p>Thanks!</p>