diff --git a/src/sbbs3/main.cpp b/src/sbbs3/main.cpp
index 83c63eff849d2e3934994d69292adfd5c99501bf..36cba96d460e6c095f201b54b799d685723cb927 100644
--- a/src/sbbs3/main.cpp
+++ b/src/sbbs3/main.cpp
@@ -416,11 +416,15 @@ static bool read_socket(SOCKET sock,unsigned char *buffer,int len)
 }
 
 /* Convert a binary variable into a hex string - used for printing in the debug log */
-static void btox(char *hexstr, const unsigned char *srcbuf, int srcbuflen) 
+static void btox(char *hexstr, const unsigned char *srcbuf, int srcbuflen, int hexstrlen) 
 {
-	int i;
-	for (i=0;i<srcbuflen;i++) sprintf(hexstr+i*2,"%02X",srcbuf[i]);
-	hexstr[i*2] = 0; // null terminate the string
+	if (hexstrlen < srcbuflen*2) {
+		lprintf(LOG_WARNING,"btox Hexstr buffer too small [%d] - not all data will be shown",hexstrlen);
+		srcbuflen = hexstrlen/2;
+	}
+
+	*hexstr = '\0';
+	for (int i=0;i<srcbuflen;i++) sprintf(hexstr+strlen(hexstr),"%02X",srcbuf[i]);
 }
 
 /* TODO: IPv6 */
@@ -5609,7 +5613,7 @@ NO_SSH:
 
 				// OK, just for sanity, our next 6 chars should be v2...
 				if (read_socket(client_socket,hapstr,6)==false || strcmp((char *)hapstr,"\nQUIT\n") != 0) {
-					btox(haphex,hapstr,6);
+					btox(haphex,hapstr,6,sizeof(haphex));
 					lprintf(LOG_ERR,"%04d * HAPROXY Something went wrong [%s] incomplete v2 setup",client_socket,haphex);
 					close_socket(client_socket);
 					continue;
@@ -5617,7 +5621,7 @@ NO_SSH:
 
 				// Command and Version
 				if (read_socket(client_socket,hapstr,1)==false) {
-					btox(haphex,hapstr,1);
+					btox(haphex,hapstr,1,sizeof(haphex));
 					lprintf(LOG_ERR,"%04d * HAPROXY, looking for Verson/Command - failed [%s]",client_socket,haphex);
 					close_socket(client_socket);
 					continue;
@@ -5627,7 +5631,7 @@ NO_SSH:
 
 				// Protocol and Family
 				if (read_socket(client_socket,hapstr,1)==false) {
-					btox(haphex,hapstr,1);
+					btox(haphex,hapstr,1,sizeof(haphex));
 					lprintf(LOG_ERR,"%04d * HAPROXY, looking for Protocol/Family - failed [%s]",client_socket,haphex);
 					close_socket(client_socket);
 					continue;
@@ -5638,7 +5642,7 @@ NO_SSH:
 
 				// Address Length - 2 bytes
 				if (read_socket(client_socket,hapstr,2)==false) {
-					btox(haphex,hapstr,2);
+					btox(haphex,hapstr,2,sizeof(haphex));
 					lprintf(LOG_ERR,"%04d * HAPROXY, looking for address length - failed [%s]",client_socket,haphex);
 					close_socket(client_socket);
 					continue;
@@ -5656,7 +5660,7 @@ NO_SSH:
 						}
 
 						if (read_socket(client_socket,hapstr,i)==false) {
-							btox(haphex,hapstr,i);
+							btox(haphex,hapstr,i,sizeof(haphex));
 							lprintf(LOG_ERR,"%04d * HAPROXY looking for IPv4 address - failed [%s]",client_socket,haphex);
 							close_socket(client_socket);
 							continue;
@@ -5674,7 +5678,7 @@ NO_SSH:
 						}
 
 						if (read_socket(client_socket,hapstr,i)==false) {
-							btox(haphex,hapstr,i);
+							btox(haphex,hapstr,i,sizeof(haphex));
 							lprintf(LOG_ERR,"%04d * HAPROXY looking for IPv6 address - failed [%s]",client_socket,haphex);
 							close_socket(client_socket);
 							continue;