@SuppressWarnings("unchecked")
	public void run() {
		log.info("Attempting to connect to chat server...");
		try {
			socket = new Socket(plugin.getConfig().getString("chat-server"), 51325);
			out = new DataOutputStream(socket.getOutputStream());
			in = new DataInputStream(socket.getInputStream());
		} catch (UnknownHostException e) {
			log.warning("Chat server: Unknown host");
			MCNSAChat3.thread = null;
			return;
		} catch (IOException e) {
			log.warning("Couldn't connect to chat server");
			MCNSAChat3.thread = null;
			return;
		}

		connected = true;
		log.info("Connected to chat server.");
		System.out.println("Connected to chat server.");

		try {
			new ServerJoinedPacket(plugin.name, plugin.longname, PlayerManager.getPlayersByServer(plugin.name)).write(out);
			new ChannelListingPacket(ChannelManager.channels).write(out);

			while (loop(in, out))
				;
		} catch (IOException e) {
			log.warning("Chat server: connection lost?");
			String msg = "";
			ArrayList<ChatPlayer> players = (ArrayList<ChatPlayer>) PlayerManager.players.clone();
			for (ChatPlayer p : players) {
				if (!p.server.equals(plugin.name)) {
					msg += p.name + " ";
					PlayerManager.players.remove(p);
				}
			}
			log.warning("Players lost: " + msg);

			MCNSAChat3.thread = null;
			return;
		} finally {
			try {
				if (out != null)
					out.close();
				if (in != null)
					in.close();
				socket.close();
				log.info("Disconnected from chat server.");
			} catch (IOException e) {
				log.warning("Error closing socket");
			}
		}
		MCNSAChat3.thread = null;
	}