diff -uNr sersniff-0.0.2/sersniff.c sersniff-0.0.2-kees-1/sersniff.c
--- sersniff-0.0.2/sersniff.c	Sat Nov 27 19:25:31 1999
+++ sersniff-0.0.2-kees-1/sersniff.c	Sat Dec  4 13:25:54 1999
@@ -22,6 +22,7 @@
 
 	07/09/1999 - Started writing.
 	21Nov1999  - Cook: added command line support and extra error checking
+	27Nov1999  - Cook: added select, timer & changed output look
 */
 
 #define VERSION "0.0.2"
@@ -32,9 +33,14 @@
 #include <termios.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <time.h>
+#include <sys/time.h>
+#include <errno.h>
 
 #include "tcp.h"
 
+#define USEC 1000000
+#define SCREEN_WIDTH 80
 char * speed_str[] = { "300", "1200", "2400", "4800", "9600", "19200", "38400",
 		"57600", "115200", "230400", NULL };
 speed_t speed_num[] = { B300, B1200, B2400, B4800, B9600, B19200, B38400,
@@ -76,55 +82,185 @@
 	return close(filedes);
 }
 
-void outputchar(unsigned char c)
-{
-	if ((c < 32) | (c > 126)) {
+/*
+   this returns the string for the character passed to it
+   It could be expanded in the future to maybe to string recognitions?
+*/
+char * chardecide(unsigned char c, int alpha) {
+	static char result[256];
+
+	/* everyone should take up 5 characters */
+	if (alpha) {
+  	   if ((c < 32) | (c > 126)) {
 		switch (c) {
 			case 10:
-				printf("<LF>");
+				sprintf(result,"<LF>");
 				break;
 			case 13:
-				printf("<CR>");
+				sprintf(result,"<CR>");
 				break;
 			case 27:
-				printf("<ESC>");
+				sprintf(result,"<ESC>");
 				break;
 			default:
-				printf("<%02hx>", c);
+				snprintf(result,256,"<%02hX>",c);
+				break;
 		}
-	} else {
-		printf("%c", c);
+	   } else {
+		snprintf(result,256,"%c",c);
+	   }
+	}
+	else {
+	   snprintf(result,256,"0x%02hX ",c);
+	}
+	return result;
+}
+
+void outputchar(unsigned char c, int port, int alpha,
+		long usec_threshold, long usec_waited)
+{
+	/* where to start wrap-counts (needs to include the "Port1:\t") */
+	#define WRAPINIT 9
+	/* record of which port we displayed last */
+	static int last=-1;
+	/* count of characters on the current line */
+	static int wrap=WRAPINIT;
+	char * todisplay;
+	int displaylen;
+
+	if (usec_waited>usec_threshold) {
+		/* report how long we waited between the last port */
+		if (wrap!=WRAPINIT) printf("\n");
+		printf("\t<waited %0ld.%0ld seconds>\n",
+			usec_waited / USEC,
+			usec_waited % USEC);
+		wrap=WRAPINIT;
 	}
 
-	if (c==0) fflush(NULL);
-	fflush(NULL);
+	/* we need to alternate which port is reporting */
+	if (port!=last) {
+		/* if we didn't just send a CR, we need to now */
+		if (wrap!=WRAPINIT) printf("\n");
+		printf("Port%d:\t",port);
+		last=port;
+		wrap=WRAPINIT;
+	}
+	else if (wrap==WRAPINIT) {
+		/* we should indent since we're continuing the same
+		   port's report */
+		printf("\t");
+	}
+
+	todisplay=chardecide(c,alpha);
+	/* increase our wrap counter, by the size of the string */
+	displaylen=strlen(todisplay);
+
+	/* if we would go over the line, wrap, then print */
+	if (displaylen+wrap > SCREEN_WIDTH) {
+		wrap=WRAPINIT;
+		printf("\n\t");
+	}
+
+	/* now print out what we want */
+	wrap+=displaylen;
+	printf("%s",todisplay);
+	
+	/* check for wrapping */
+	if (wrap % SCREEN_WIDTH == 0) {
+		/* we hit the edge of the screen */
+		wrap=WRAPINIT;
+		printf("\n");
+	}
+
+	fflush(NULL); /* flush all the time */
 }
 
-void mainloop(int port1, int port2, int silent)
+void mainloop(int port1, int port2, int silent, int alpha, long usec_threshold)
 {
 	unsigned char c1, c2;
 	int last;
+	int rc;
+	fd_set rfds;
+	fd_set efds;
+	int biggestfd=0;
+	int fdret;
+	struct timeval before;
+	struct timeval after;
+	long timediff;
 	int quit=0;
 
+	/* need the largest fd for the select call */
+	biggestfd=port1 > port2 ? port1 : port2;
+	biggestfd++;
 	last=0;
 
 	while (!quit) {
-		if (read(port1, &c1, 1)==1) {
-			if (last!=1) {
-				last=1;
-				printf("\nPort1: ");
+		/* reset the select set */
+		FD_ZERO(&rfds);
+		FD_ZERO(&efds);
+		FD_SET(port1,&rfds);
+		FD_SET(port1,&efds);
+		FD_SET(port2,&rfds);
+		FD_SET(port2,&efds);
+
+		if (gettimeofday(&before,NULL)) {
+			perror("gettimeofday");
+			exit(1);
+		}
+		if ((fdret=select(biggestfd, &rfds, NULL, &efds, NULL))<0) {
+			perror("select");
+			exit(1);
+		}
+		if (gettimeofday(&after,NULL)) {
+			perror("gettimeofday");
+			exit(1);
+		}
+
+		/* get seconds difference */
+		timediff=after.tv_sec-before.tv_sec;
+		/* convert to micro seconds */
+		timediff*=USEC;
+		/* add difference in usecs */
+		timediff+=after.tv_usec-before.tv_usec;
+		
+		if (FD_ISSET(port1, &rfds)) {
+			for (rc=read(port1, &c1, 1);
+			     rc>0; rc=read(port1, &c1, 1) ) {
+				outputchar(c1,1,alpha,usec_threshold,timediff);
+				timediff=0;
+				if (!silent) write(port2,&c1,1);
+			}	
+			if (rc<0 && errno!=EAGAIN) {
+				perror("read(port1)");
+				exit(1);
 			}
-			outputchar(c1);
-			if (!silent) write(port2, &c1, 1);
 		}
 		
-		if (read(port2, &c2, 1)==1) {
-			if (last!=2) {
-				last=2;
-				printf("\nPort2: ");
+		if (FD_ISSET(port2, &rfds)) {
+			for (rc=read(port2, &c2, 1);
+			     rc>0; rc=read(port2, &c2, 1) ) {
+				outputchar(c2,2,alpha,usec_threshold,timediff);
+				timediff=0;
+				if (!silent) write(port1,&c2,1);
+			}	
+			if (rc<0 && errno!=EAGAIN) {
+				perror("read(port2)");
+				exit(1);
 			}
-			outputchar(c2);
-			if (!silent) write(port1, &c2, 1);
+		}
+
+		/* check for exceptions (sockets closed, broken, etc) */
+		if (FD_ISSET(port1, &efds)) {
+			/* I can't remember right now what to actually
+			   check for on a fd exception, so we'll just quit */
+			fprintf(stderr,"\nException on port1\n");
+			quit=1;
+		}
+		if (FD_ISSET(port2, &efds)) {
+			/* I can't remember right now what to actually
+			   check for on a fd exception, so we'll just quit */
+			fprintf(stderr,"\nException on port2\n");
+			quit=1;
 		}
 	}
 
@@ -136,19 +272,20 @@
 {
 	fprintf(stderr,"sersniff v%s
 
-Usage: sersniff [-h] [-i DEVICE | -l PORT] [-o DEVICE | -c HOST:PORT] [-b BAUD]
-
+Usage:
+sersniff [-h] [-i DEV | -l PORT] [-o DEV | -c HOST:PORT] [-b BAUD] [-w USEC]
 -h		This help
--i IN_DEV	Port 1 device (defaults to /dev/ttyS0)
+-x		Show hex characters instead of alpha
+-i DEVICE	Port 1 device (defaults to /dev/ttyS0)
 -l PORT		Port 1 port for TCP
--o OUT_DEV	Port 2 device (defaults to /dev/ttyS1)
+-o DEVICE	Port 2 device (defaults to /dev/ttyS1)
 -c HOST:PORT	Port 2 host & port to connect to
 -b BAUD		Baud rate (Defaults to 19200)
--s		Silent - don't pass data from port1 <=> port2,
+-w USECS	How many microsecs to wait before reporting a delay
+			(default is %d)
+-s 		Silent - don't pass data from port1 <=> port2,
 			just display what we see from them.
-
-",VERSION);
-
+",VERSION,USEC);
 	exit(1);
 }
 
@@ -161,16 +298,21 @@
 	int listenport=0;
 	int connectport;
 	char *connecthost=NULL, *tmpchr=NULL;
+	int show_alpha=1;
 	speed_t baud=B0;
 	int silent=0;
+	long usec_threshold=USEC;
 
-	while ((optret=getopt(argc,argv,"hi:l:o:c:b:s"))!=EOF) {
+	while ((optret=getopt(argc,argv,"hxsi:l:o:c:b:w:"))!=EOF) {
 		switch (optret) {
 		case '?': case 'h': case ':':
 			usage();
 		case 's':
 			silent=1;
 			break;
+		case 'w':
+			usec_threshold=atoi(optarg);
+			break;
 		case 'i':
 			dev1=strdup(optarg);
 			break;
@@ -182,13 +324,15 @@
 			break;
 		case 'c':
 			if ((tmpchr=strchr(optarg, ':'))==NULL) {
-				printf("Must specify -c option with host:port\n");
-				exit(1);
+			    printf("Must specify -c option with host:port\n");
+                            exit(1);
 			}
+			*tmpchr='\0';
 			connectport=atoi(++tmpchr);
 			connecthost=strdup(optarg);
-			tmpchr=strchr(connecthost, ':');
-			*tmpchr=0;
+			break;
+		case 'x':
+			show_alpha=0;
 			break;
 		case 'b':
 			for (speed=0;
@@ -210,31 +354,32 @@
 
 	/* Default settings */
 	if (!dev1 && !listenport) dev1=strdup("/dev/ttyS0");
-	if (!dev2 && connecthost==NULL) dev2=strdup("/dev/ttyS1");
+	if (!dev2 && !connecthost) dev2=strdup("/dev/ttyS1");
 	if (baud==B0) baud=B19200;
 
-	if (dev1!=NULL) {
+	if (dev1) {
 		port1=openport(dev1, baud);
 	} else {
 		port1=listensock(listenport);
 	}
-	
-	if (dev2!=NULL) {
+
+	if (dev2) {
 		port2=openport(dev2, baud);
 	} else {
 		port2=opensock(connecthost, connectport);
-	};
+	}
 
-	if (port1==-1 || port2==-1) {
-		printf("Argh. Got -1. Die!\n");
+	if (port1 < 0 || port2 < 0) {
+		fprintf(stderr,"Argh.  An open failed!\n");
 		exit(1);
 	}
 
-	mainloop(port1, port2, silent);
+	mainloop(port1, port2, silent, show_alpha, usec_threshold);
 
 	/* Clean up */
-	free(dev1);
-	free(dev2);
+	if (dev1) free(dev1);
+	if (dev2) free(dev2);
+	if (connecthost) free(connecthost);
 
 	return 0;
 }
diff -uNr sersniff-0.0.2/tcp.c sersniff-0.0.2-kees-1/tcp.c
--- sersniff-0.0.2/tcp.c	Sat Nov 27 18:47:12 1999
+++ sersniff-0.0.2-kees-1/tcp.c	Sat Dec  4 12:37:18 1999
@@ -51,9 +51,16 @@
 	int sock, newsock;
 	struct sockaddr_in saddr;
 	int saddrlen;
+	int on;
 
 	if ((sock=socket(AF_INET, SOCK_STREAM, 0))==-1) {
 		perror("socket()");
+		return -1;
+	}
+
+	on=1;
+	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
+		perror("setsockopt(SO_REUSEADDR)");
 		return -1;
 	}
 
