diff -r -u soundmodem-0.7/soundcard/kisspkt.c soundmodem-0.7b/soundcard/kisspkt.c
--- soundmodem-0.7/soundcard/kisspkt.c	2003-11-27 18:19:35.000000000 -0800
+++ soundmodem-0.7b/soundcard/kisspkt.c	2004-01-15 18:03:14.000000000 -0800
@@ -196,22 +196,41 @@
 
 static void hdlc_encode(struct modemchannel *chan, unsigned char *pkt, unsigned int len)
 {
-        unsigned bitstream, notbitstream, bitbuf, numbit;
+        unsigned bitstream, notbitstream, bitbuf;
+	signed numbit;
 	unsigned wr = chan->pkt.htx.wr;
+	unsigned repack = 0;
+	unsigned sparebits, usedbits;
 
 	chan->pkt.stat.pkt_out++;
 	append_crc_ccitt(pkt, len);
 	len += 2;
 	bitstream = 0;
-	bitbuf = 0x7e;
-	numbit = 8; /* opening flag */
+	sparebits = chan->pkt.htx.sparebits;
+	usedbits = 8 - sparebits;
+	if (chan->pkt.htx.flagged) {
+		bitbuf = 0;
+		numbit = 0;
+	} else {
+		bitbuf = 0x7e;
+		numbit = 8; /* opening flag */
+	}
+	if (sparebits != 0) {
+	        wr = wr ? (wr - 1) : (TXBUFFER_SIZE - 1);
+	        bitbuf <<= usedbits;
+		bitbuf |= chan->pkt.htx.buf[wr];
+		numbit += usedbits;
+		chan->pkt.htx.sparebits = 0;
+		repack = 1;
+	}
 	while (numbit >= 8) {
 		chan->pkt.htx.buf[wr] = bitbuf;
 		wr = (wr + 1) % TXBUFFER_SIZE;
-		if (wr == chan->pkt.htx.rd)
+		if (wr == chan->pkt.htx.rd && !repack)
 			*(int *)0 = 0;  /* must not happen! */
 		bitbuf >>= 8;
 		numbit -= 8;
+		repack = 0;
 	}
  	for (; len > 0; len--, pkt++) {
 		bitstream >>= 8;
@@ -248,7 +267,7 @@
 	}
 	bitbuf |= 0x7e7e << numbit;
 	numbit += 16;
-	while (numbit >= 8) {
+	while (numbit > 0) {
 		chan->pkt.htx.buf[wr] = bitbuf;
 		wr = (wr + 1) % TXBUFFER_SIZE;
 		if (wr == chan->pkt.htx.rd)
@@ -256,7 +275,9 @@
 		bitbuf >>= 8;
 		numbit -= 8;
 	}
+	chan->pkt.htx.sparebits = -numbit;
 	chan->pkt.htx.wr = wr;
+	chan->pkt.htx.flagged = 1;
 }
 
 /* ---------------------------------------------------------------------- */
@@ -556,10 +577,15 @@
 	unsigned int i, j, n = len;
 
 	i = (chan->pkt.htx.rd - chan->pkt.htx.wr - 1) % TXBUFFER_SIZE;
-	if (i > KISSINBUF_SIZE*6/5)
+	if (i > KISSINBUF_SIZE*6/5 - 5)
 		kiss_input(chan);
-	if (chan->pkt.inhibittx || chan->pkt.htx.rd == chan->pkt.htx.wr)
+	if (chan->pkt.inhibittx)
+		return 0;
+	if (chan->pkt.htx.rd == chan->pkt.htx.wr) {
+		chan->pkt.htx.sparebits = 0;
+		chan->pkt.htx.flagged = 0;
 		return 0;
+	}
 	while (n > 0) {
 		if (chan->pkt.htx.wr >= chan->pkt.htx.rd)
 			j = chan->pkt.htx.wr - chan->pkt.htx.rd;
diff -r -u soundmodem-0.7/soundcard/kisspkt.h soundmodem-0.7b/soundcard/kisspkt.h
--- soundmodem-0.7/soundcard/kisspkt.h	2002-08-09 13:01:52.000000000 -0700
+++ soundmodem-0.7b/soundcard/kisspkt.h	2004-01-15 17:57:44.000000000 -0800
@@ -59,6 +59,8 @@
 
 	struct {
 		unsigned rd, wr;
+	        unsigned int sparebits;
+	   	unsigned int flagged;
 		unsigned char buf[TXBUFFER_SIZE];
 	} htx;
 	
