anchor.diff:  		{
   			print("message from client: " + message);
   			processMessage(ws, message);
  -	    });
  +	    }, onError: (error)
  +		{
  +			cleanupLists(ws);
  +		}, onDone: ()
  +		{
  +			cleanupLists(ws);
  +		});
  +	}
  +	
  +	void cleanupLists(WebSocket ws)
  +	{
  +		List<String> socketRemove = new List<String>();
  +		List<int> usersRemove = new List<int>();
  +		String leavingUser;
  +		userSockets.forEach((String username, WebSocket socket)
  +		{
  +			if(socket == ws)
  +			{
  +				socketRemove.add(username);
  +				leavingUser = username.substring(0, username.indexOf("_"));
  +				users.removeWhere((Identifier userId) => userId.username == leavingUser);
  +			}
  +		});
  +		socketRemove.forEach((String username)
  +		{
  +			userSockets.remove(username);
  +		});
  +		
  +		//send a message to all other clients that this user has disconnected
  +		Map map = new Map();
  +		map["message"] = " left.";
  +		map["username"] = leavingUser;
  +		sendAll(JSON.encode(map));
   	}
   
   	processMessage(WebSocket ws, String receivedMessage) 
   	{
   		try 
   		{
  -			String userName = getUserName(ws);
   			Map map = JSON.decode(receivedMessage);
   			
   			if(map["username"] == null) 
   			{
   				//combine the username with the channel name to keep track of the same user in multiple channels
  -				userName = map["message"].substring(9)+"_"+map["channel"];
  -				if(users[userName] != null) 
  +				String userName = map["message"].substring(9)+"_"+map["channel"];
  +				if(userSockets[userName] != null) 
   				{
  -			    	users[userName].close();  //  close the previous connection
  +			    	userSockets[userName].close();  //  close the previous connection
           		}
  -    			users[userName] = ws;
  +    			userSockets[userName] = ws;
   				map["username"] = map["message"].substring(9);
       			map["message"] = ' joined.';
  +				users.add(new Identifier(map["username"],map["channel"]));
     			}
   			else if(map["statusMessage"] == "changeName")
   			{
  -				users[map["username"]] = users[map["newUername"]];
  -				users.remove(map["username"]);
  +				bool success = true;
  +				users.forEach((Identifier userId)
  +				{
  +					if(userId.username == map["newUsername"])
  +						success = false;
  +				});
  +				
  +				if(!success)
  +				{
  +					Map errorResponse = new Map();
  +					errorResponse["statusMessage"] = "changeName";
  +					errorResponse["success"] = "false";
  +					errorResponse["message"] = "This name is already taken.  Please choose another.";
  +					errorResponse["channel"] = map["channel"];
  +					userSockets[map["username"]+"_"+map["channel"]].add(JSON.encode(errorResponse));
  +					return;
  +				}
  +				else
  +				{
  +					map["success"] = "true";
  +					map["message"] = "is now known as";
  +					map["channel"] = "all"; //echo it back to all channels so we can update the connectedUsers list on the client's side
  +					
  +					users.forEach((Identifier userId)
  +					{
  +						if(userId.username == map["username"]) //update the old usernames
  +						{
  +							String usernameWithChannel = userId.username+"_"+userId.channelName;
  +							userSockets[map["newUsername"]+"_"+userId.channelName] = userSockets.remove(usernameWithChannel);
  +							userId.username = map["newUsername"];
  +						}
  +					});
  +				}
  +			}
  +			else if(map["statusMessage"] == "list")
  +			{
  +				List userList = new List();
  +				users.forEach((Identifier userId)
  +				{
  +					if(!userList.contains(userId.username) && userId.channelName == map["channel"])
  +						userList.add(userId.username);
  +				});
  +				map["users"] = userList;
  +				map["message"] = "Users in this channel: ";
  +				userSockets[map["username"]+"_"+map["channel"]].add(JSON.encode(map));
  +				return;
   			}
   			
         		sendAll(JSON.encode(map));
  
positive.code: void main() 
  {
  	WebSocketHandler webSocketHandler = new WebSocketHandler();
  
  	HttpServer.bind('0.0.0.0', int.parse(Platform.environment['PORT'])).then((HttpServer server) 
  	{
  		server.listen((HttpRequest request)
  		{
  			WebSocketTransformer.upgrade(request).then((WebSocket websocket)
  			{
       			webSocketHandler.wsHandler(websocket);
  			});
  	    });
  			
  		print('${new DateTime.now().toString()} - Serving Chat on ${'0.0.0.0'}:${Platform.environment['PORT']}.');
  	});
  
negative.code:     	}
  	}
  
    	void sendAll(String sendMessage) 
  	{
  	    userSockets.forEach((String username, WebSocket socket) 
  		{
  			if(socket != null)
  				socket.add(sendMessage);
  	    });
    	}
  }
  