diff --git a/src/syncterm/conn_pty.c b/src/syncterm/conn_pty.c
index 49bc9e14377ea7c56db7443f1679e06703ba4251..3d4d4071e5a8e4c2dda0913c17a23716b0d6c8bd 100644
--- a/src/syncterm/conn_pty.c
+++ b/src/syncterm/conn_pty.c
@@ -308,6 +308,7 @@ pty_input_thread(void *args)
 	int    buffered;
 	size_t buffer;
 	int    i;
+	struct timeval tv;
 
 	SetThreadName("PTY Input");
 	conn_api.input_thread_running = 1;
@@ -316,16 +317,9 @@ pty_input_thread(void *args)
 			break;
 		FD_ZERO(&rds);
 		FD_SET(master, &rds);
-#ifdef __linux__
-		{
-			struct timeval tv;
-			tv.tv_sec = 0;
-			tv.tv_usec = 500000;
-			rd = select(master + 1, &rds, NULL, NULL, &tv);
-		}
-#else
-		rd = select(master + 1, &rds, NULL, NULL, NULL);
-#endif
+		tv.tv_sec = 0;
+		tv.tv_usec = 100000;
+		rd = select(master + 1, &rds, NULL, NULL, &tv);
 		if (rd == -1) {
 			if (errno == EBADF)
 				break;
@@ -358,6 +352,7 @@ pty_output_thread(void *args)
 	int    wr;
 	int    ret;
 	int    sent;
+	struct timeval tv;
 
 	SetThreadName("PTY Output");
 	conn_api.output_thread_running = 1;
@@ -371,20 +366,12 @@ pty_output_thread(void *args)
 			wr = conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size);
 			pthread_mutex_unlock(&(conn_outbuf.mutex));
 			sent = 0;
-			while (sent < wr) {
+			while (master != -1 && sent < wr) {
 				FD_ZERO(&wds);
 				FD_SET(master, &wds);
-#ifdef __linux__
-				{
-					struct timeval tv;
-
-					tv.tv_sec = 0;
-					tv.tv_usec = 500000;
-					ret = select(master + 1, NULL, &wds, NULL, &tv);
-				}
-#else
-				ret = select(master + 1, NULL, &wds, NULL, NULL);
-#endif
+				tv.tv_sec = 0;
+				tv.tv_usec = 100000;
+				ret = select(master + 1, NULL, &wds, NULL, &tv);
 				if (ret == -1) {
 					if (errno == EBADF)
 						break;
@@ -514,6 +501,7 @@ pty_close(void)
 {
 	time_t start;
 	char   garbage[1024];
+	int oldmaster;
 
 	conn_api.terminate = 1;
 	start = time(NULL);
@@ -527,10 +515,13 @@ pty_close(void)
 	kill(child_pid, SIGKILL);
 	waitpid(child_pid, &status, 0);
 
+	oldmaster = master;
+	master = -1;
 	while (conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1) {
 		conn_recv_upto(garbage, sizeof(garbage), 0);
 		SLEEP(1);
 	}
+	master = oldmaster;
 	destroy_conn_buf(&conn_inbuf);
 	destroy_conn_buf(&conn_outbuf);
 	FREE_AND_NULL(conn_api.rd_buf);
diff --git a/src/syncterm/modem.c b/src/syncterm/modem.c
index 607a31c4faede6477d9564850295a384a3f04019..0a9fd6fcd0e462fef0bb5ef42a3167c021f370a4 100644
--- a/src/syncterm/modem.c
+++ b/src/syncterm/modem.c
@@ -33,7 +33,7 @@ modem_input_thread(void *args)
 	while (com != COM_HANDLE_INVALID && !conn_api.terminate) {
 		rd = comReadBuf(com, (char *)conn_api.rd_buf, conn_api.rd_buf_size, NULL, 100);
 		buffered = 0;
-		while (buffered < rd) {
+		while (com != COM_HANDLE_INVALID && buffered < rd) {
 			pthread_mutex_lock(&(conn_inbuf.mutex));
 			buffer = conn_buf_wait_free(&conn_inbuf, rd - buffered, 100);
 			buffered += conn_buf_put(&conn_inbuf, conn_api.rd_buf + buffered, buffer);
@@ -74,7 +74,7 @@ modem_output_thread(void *args)
 			wr = conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size);
 			pthread_mutex_unlock(&(conn_outbuf.mutex));
 			sent = 0;
-			while (sent < wr) {
+			while (com != COM_HANDLE_INVALID && sent < wr) {
 				ret = comWriteBuf(com, conn_api.wr_buf + sent, wr - sent);
 				sent += ret;
 				if (ret == COM_ERROR)
@@ -357,6 +357,7 @@ modem_close(void)
 {
 	time_t start;
 	char   garbage[1024];
+	COM_HANDLE oldcom;
 
 	conn_api.terminate = 1;
 
@@ -370,11 +371,14 @@ modem_close(void)
 		goto CLOSEIT;
 
 	start = time(NULL);
+	oldcom = com;
+	com = COM_HANDLE_INVALID;
 	while (time(NULL) - start <= 10) {
 		if ((comGetModemStatus(com) & COM_DCD) == 0)
 			goto CLOSEIT;
 		SLEEP(1000);
 	}
+	com = oldcom;
 
 CLOSEIT:
 	while (conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1) {
diff --git a/src/syncterm/ripper.c b/src/syncterm/ripper.c
index b5edc3490f44a489b4071f191c47c63734edc484..cc5b14b5caca394eaf339f24246020676d8a6459 100644
--- a/src/syncterm/ripper.c
+++ b/src/syncterm/ripper.c
@@ -8,6 +8,7 @@
 #endif
 #include <bitmap_con.h>
 #include <ciolib.h>
+#include <datewrap.h>
 #include <dirwrap.h>
 #include <gen_defs.h>
 #include <genwrap.h>
diff --git a/src/syncterm/rlogin.c b/src/syncterm/rlogin.c
index 3bc0e1bf1988a21ec60f2bda9bd07755d49803a3..432b470a0240f27e635f038456c6f5aac00e11ee 100644
--- a/src/syncterm/rlogin.c
+++ b/src/syncterm/rlogin.c
@@ -25,13 +25,13 @@ rlogin_input_thread(void *args)
 	SetThreadName("RLogin Input");
 	conn_api.input_thread_running = 1;
 	while (rlogin_sock != INVALID_SOCKET && !conn_api.terminate) {
-		if (socket_readable(rlogin_sock, -1)) {
+		if (socket_readable(rlogin_sock, 100)) {
 			rd = recv(rlogin_sock, conn_api.rd_buf, conn_api.rd_buf_size, 0);
 			if (rd <= 0)
 				break;
 		}
 		buffered = 0;
-		while (buffered < rd) {
+		while (rlogin_sock != INVALID_SOCKET && buffered < rd) {
 			pthread_mutex_lock(&(conn_inbuf.mutex));
 			buffer = conn_buf_wait_free(&conn_inbuf, rd - buffered, 1000);
 			buffered += conn_buf_put(&conn_inbuf, conn_api.rd_buf + buffered, buffer);
@@ -62,8 +62,8 @@ rlogin_output_thread(void *args)
 			wr = conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size);
 			pthread_mutex_unlock(&(conn_outbuf.mutex));
 			sent = 0;
-			while (sent < wr) {
-				if (socket_writable(rlogin_sock, -1)) {
+			while (rlogin_sock != INVALID_SOCKET && sent < wr) {
+				if (socket_writable(rlogin_sock, 100)) {
 					ret = sendsocket(rlogin_sock, conn_api.wr_buf + sent, wr - sent);
 					if (ret == -1)
 						break;
@@ -210,13 +210,16 @@ int
 rlogin_close(void)
 {
 	char garbage[1024];
+	SOCKET oldsock;
 
 	conn_api.terminate = 1;
-	closesocket(rlogin_sock);
+	oldsock = rlogin_sock;
+	rlogin_sock = INVALID_SOCKET;
 	while (conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1) {
 		conn_recv_upto(garbage, sizeof(garbage), 0);
 		SLEEP(1);
 	}
+	closesocket(oldsock);
 	destroy_conn_buf(&conn_inbuf);
 	destroy_conn_buf(&conn_outbuf);
 	FREE_AND_NULL(conn_api.rd_buf);
diff --git a/src/syncterm/ssh.c b/src/syncterm/ssh.c
index dee309462e2aafc501b1366ed4eefdb524d9ca39..a1d3066db851d6e814877b41f8f99ba8ec366168 100644
--- a/src/syncterm/ssh.c
+++ b/src/syncterm/ssh.c
@@ -54,7 +54,7 @@ ssh_input_thread(void *args)
 	SetThreadName("SSH Input");
 	conn_api.input_thread_running = 1;
 	while (ssh_active && !conn_api.terminate) {
-		if (!socket_readable(ssh_sock, 10))
+		if (!socket_readable(ssh_sock, 100))
 			continue;
 
 		pthread_mutex_lock(&ssh_mutex);
@@ -105,7 +105,7 @@ ssh_output_thread(void *args)
 			wr = conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size);
 			pthread_mutex_unlock(&(conn_outbuf.mutex));
 			sent = 0;
-			while (sent < wr) {
+			while (ssh_active && sent < wr) {
 				pthread_mutex_lock(&ssh_mutex);
 				status = cl.PushData(ssh_session, conn_api.wr_buf + sent, wr - sent, &ret);
 				pthread_mutex_unlock(&ssh_mutex);
@@ -352,8 +352,8 @@ ssh_close(void)
 	char garbage[1024];
 
 	conn_api.terminate = 1;
-	ssh_active = true;
 	cl.SetAttribute(ssh_session, CRYPT_SESSINFO_ACTIVE, 0);
+	ssh_active = false;
 	while (conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1) {
 		conn_recv_upto(garbage, sizeof(garbage), 0);
 		SLEEP(1);