 
 		# Map from docName -> {listener:fn, queue:[msg], busy:bool}
 		docState = {}
+		closed = false
 
 		# Send a message to the client.
 		# msg _must_ have the doc:DOCNAME property set. We'll remove it if its the same as lastReceivedDoc.

		client.on 'message', messageListener
		client.on 'disconnect', ->
			p "client #{client.sessionId} disconnected"
			for docName, state of docState
				state.busy = true
				state.queue = []
				model.removeListener docName, state.listener if state.listener?
			client.removeListener 'message', messageListener
			docState = null
	
	server

		messageListener = (query) ->
			# There seems to be a bug in socket.io where messages are detected
			# after the client disconnects.
			if closed
				console.log "WARNING: received query from client after the client disconnected."
				console.log client
				return
