public synchronized void close() throws IOException {
		if (socket != null) {
			if (this.connected) {
				this.closing = true;

				// try to send outgoing data for at most 10ms
				if (writeThread != null) {
					try {
						writeThread.join(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					} finally {
						writeThread = null;
					}
				}
				if (readThread != null) {
					readThread.interrupt();
					readThread = null;
				}
				socket.close();

				Thread.yield();

				this.socket = null;
				this.connected = false;

				doDisconnect();
			}
		}
	}