--- imap.inc.ORIG	2009-09-29 16:44:59.000000000 -0500
+++ imap.inc	2009-09-29 16:44:33.000000000 -0500
@@ -252,6 +252,18 @@
     		$line .= $buffer;
 	} while ($buffer[strlen($buffer)-1] != "\n");
 
+	// Dovecot 1.2.4: "BYE BUG: Unknown internal error"
+	// Dovecot 1.2.5: "BYE Internal error occurred. Refer to server log for more information."
+	// FWIW: nothing in the $conn object is helpful
+	// FWIW: socket_get_status($fp) has no helpful information
+	if ($line == false) {
+		$_msg = "Unexpected condition from IMAP server, closed or corrupt connection to IMAP. Possible mailbox corruption.";
+		error_log($_msg);
+		print "$_msg";
+		fclose($fp);
+		exit;
+	}
+
 	return $line;
 }
 
@@ -1433,11 +1445,13 @@
 	$request .= "CONTENT-TRANSFER-ENCODING CONTENT-TYPE MESSAGE-ID ";
 	$request .= "REFERENCES DISPOSITION-NOTIFICATION-TO X-PRIORITY".$add.")])";
 
+error_log("iil_C_FetchHeaders' 1st iil_PutLine: $request");
 	if (!iil_PutLine($fp, $request)) {
 		return false;
 	}
 	do {
 		$line = iil_ReadLine($fp, 1024);
+error_log("iil_C_FetchHeaders' 1st iil_ReadLine: $line");
 		$line = iil_MultLine($fp, $line);
 
 		$a    = explode(' ', $line);
