diff -r d3335654e9a7 src/lib-ssl-iostream/iostream-openssl.c
--- a/src/lib-ssl-iostream/iostream-openssl.c	Wed Sep 07 17:15:10 2011 +0300
+++ b/src/lib-ssl-iostream/iostream-openssl.c	Thu Sep 08 10:18:11 2011 +0300
@@ -265,9 +265,11 @@
 		/* bytes contains how many SSL encrypted bytes we should be
 		   sending out */
 		max_bytes = o_stream_get_buffer_avail_size(ssl_io->plain_output);
+		i_debug("ssl-iostream: write pending %d bytes, max=%d", (int)bytes, (int)max_bytes);
 		if (bytes > max_bytes) {
 			if (max_bytes == 0) {
 				/* wait until output buffer clears */
+				i_debug("ssl-iostream: waiting for available output");
 				o_stream_set_flush_pending(ssl_io->plain_output,
 							   TRUE);
 				break;
@@ -288,11 +290,13 @@
 		sent = o_stream_send(ssl_io->plain_output, buffer, bytes);
 		if (sent < 0) {
 			i_assert(ssl_io->plain_output->stream_errno != 0);
+			i_debug("ssl-iostream: send failed");
 			ssl_io->ssl_output->stream_errno =
 				ssl_io->plain_output->stream_errno;
 			break;
 		}
 		i_assert(sent == (ssize_t)bytes);
+		i_debug("ssl-iostream: sent %d bytes", (int)bytes);
 		bytes_sent = TRUE;
 	}
 	o_stream_uncork(ssl_io->plain_output);
@@ -309,6 +313,7 @@
 	while ((bytes = BIO_ctrl_get_write_guarantee(ssl_io->bio_ext)) > 0) {
 		/* bytes contains how many bytes we can write to bio_ext */
 		(void)i_stream_read_data(ssl_io->plain_input, &data, &size, 0);
+		i_debug("ssl-iostream: read avail %d bytes, max %d", (int)size, (int)bytes);
 		if (size == 0) {
 			/* wait for more input */
 			break;
@@ -328,6 +333,7 @@
 	}
 	if (bytes_read) {
 		if (ssl_io->ostream_flush_waiting_input) {
+			i_debug("ssl-iostream: output flush pending");
 			ssl_io->ostream_flush_waiting_input = FALSE;
 			o_stream_set_flush_pending(ssl_io->plain_output, TRUE);
 		}
@@ -340,6 +346,7 @@
 {
 	bool ret;
 
+	i_debug("ssl-iostream: bio sync");
 	ret = ssl_iostream_bio_output(ssl_io);
 	if (ssl_iostream_bio_input(ssl_io))
 		ret = TRUE;
@@ -373,14 +380,17 @@
 	err = SSL_get_error(ssl_io->ssl, ret);
 	switch (err) {
 	case SSL_ERROR_WANT_WRITE:
+		i_debug("ssl-iostream: want_write");
 		if (!ssl_iostream_bio_sync(ssl_io)) {
 			i_panic("SSL ostream buffer size not unlimited");
 			return 0;
 		}
 		return 1;
 	case SSL_ERROR_WANT_READ:
+		i_debug("ssl-iostream: want_read");
 		ssl_io->want_read = TRUE;
 		(void)ssl_iostream_bio_sync(ssl_io);
+		i_debug("ssl-iostream: want_read -> %d", ssl_io->want_read);
 		return ssl_io->want_read ? 0 : 1;
 	case SSL_ERROR_SYSCALL:
 		/* eat up the error queue */
diff -r d3335654e9a7 src/lib-ssl-iostream/istream-openssl.c
--- a/src/lib-ssl-iostream/istream-openssl.c	Wed Sep 07 17:15:10 2011 +0300
+++ b/src/lib-ssl-iostream/istream-openssl.c	Thu Sep 08 10:18:11 2011 +0300
@@ -40,17 +40,22 @@
 			/* handshake failed */
 			stream->istream.stream_errno = errno;
 		}
+		i_debug("ssl-istream: handshake not finished");
 		return ret;
 	}
 
-	if (!i_stream_get_buffer_space(stream, 1, &size))
+	if (!i_stream_get_buffer_space(stream, 1, &size)) {
+		i_debug("ssl-istream: input buffer full");
 		return -2;
+	}
 
 	while ((ret = SSL_read(sstream->ssl_io->ssl,
 			       stream->w_buffer + stream->pos, size)) <= 0) {
 		/* failed to read anything */
+		i_debug("ssl-istream: SSL_read() failed");
 		ret = ssl_iostream_handle_error(sstream->ssl_io, ret,
 						"SSL_read");
+		i_debug("ssl-istream: SSL_read() error handling -> %d", (int)ret);
 		if (ret <= 0) {
 			if (ret < 0) {
 				stream->istream.stream_errno = errno;
@@ -61,6 +66,7 @@
 		}
 		/* we did some BIO I/O, try reading again */
 	}
+	i_debug("ssl-istream: SSL_read() = %d", (int)ret);
 	stream->pos += ret;
 	return ret;
 }
diff -r d3335654e9a7 src/lib-ssl-iostream/ostream-openssl.c
--- a/src/lib-ssl-iostream/ostream-openssl.c	Wed Sep 07 17:15:10 2011 +0300
+++ b/src/lib-ssl-iostream/ostream-openssl.c	Thu Sep 08 10:18:11 2011 +0300
@@ -82,9 +82,11 @@
 		ret = SSL_write(sstream->ssl_io->ssl,
 				CONST_PTR_OFFSET(sstream->buffer->data, pos),
 				sstream->buffer->used - pos);
+		i_debug("ssl-ostream: flush SSL_write() = %d", (int)ret);
 		if (ret <= 0) {
 			ret = ssl_iostream_handle_error(sstream->ssl_io, ret,
 							"SSL_write");
+			i_debug("ssl-ostream: flush SSL_write() failed ret=%d", (int)ret);
 			if (ret < 0) {
 				sstream->ostream.ostream.stream_errno = errno;
 				break;
@@ -107,6 +109,7 @@
 	struct ssl_ostream *sstream = (struct ssl_ostream *)stream;
 	int ret;
 
+	i_debug("ssl-ostream: flush callback");
 	if ((ret = ssl_iostream_more(sstream->ssl_io)) < 0) {
 		/* handshake failed */
 		stream->ostream.stream_errno = errno;
@@ -118,6 +121,7 @@
 
 	if (ret == 0 && sstream->ssl_io->want_read) {
 		/* we need to read more data until we can continue. */
+		i_debug("ssl-ostream: flush waiting input");
 		sstream->ssl_io->ostream_flush_waiting_input = TRUE;
 		ret = 1;
 	}
@@ -138,9 +142,11 @@
 		ret = SSL_write(sstream->ssl_io->ssl,
 				CONST_PTR_OFFSET(iov[i].iov_base, pos),
 				iov[i].iov_len - pos);
+		i_debug("ssl-ostream: SSL_write() = %d", (int)ret);
 		if (ret <= 0) {
 			ret = ssl_iostream_handle_error(sstream->ssl_io, ret,
 							"SSL_write");
+			i_debug("ssl-ostream: SSL_write() failure ret=%d", (int)ret);
 			if (ret < 0) {
 				sstream->ostream.ostream.stream_errno = errno;
 				break;
diff -r d3335654e9a7 src/lib-storage/index/imapc/imapc-connection.c
--- a/src/lib-storage/index/imapc/imapc-connection.c	Wed Sep 07 17:15:10 2011 +0300
+++ b/src/lib-storage/index/imapc/imapc-connection.c	Thu Sep 08 10:18:11 2011 +0300
@@ -899,16 +899,23 @@
 static int imapc_connection_input_one(struct imapc_connection *conn)
 {
 	const char *tag;
+	size_t size;
 	int ret = -1;
 
-	if (conn->input_callback != NULL)
+	if (conn->input_callback != NULL) {
+		i_debug("imapc: input callback");
 		return conn->input_callback(conn);
+	}
 
+	(void)i_stream_get_data(conn->input, &size);
+	i_debug("imapc: input state=%d size=%d", conn->input_state, (int)size);
 	switch (conn->input_state) {
 	case IMAPC_INPUT_STATE_NONE:
 		tag = imap_parser_read_word(conn->parser);
-		if (tag == NULL)
+		if (tag == NULL) {
+			i_debug("imapc: no tag yet");
 			return 0;
+		}
 
 		if (strcmp(tag, "*") == 0) {
 			conn->input_state = IMAPC_INPUT_STATE_UNTAGGED;
@@ -950,8 +957,11 @@
 
 	/* we need to read as much as we can with SSL streams to avoid
 	   hanging */
-	while (conn->input != NULL && (ret = i_stream_read(conn->input)) > 0)
+	i_debug("imapc: connection input input=%p", conn->input);
+	while (conn->input != NULL && (ret = i_stream_read(conn->input)) > 0) {
+		i_debug("imapc: input pending ret=%d", (int)ret);
 		imapc_connection_input_pending(conn);
+	}
 
 	if (ret < 0) {
 		/* disconnected */
@@ -966,6 +976,7 @@
 		imapc_connection_disconnect(conn);
 		return;
 	}
+	i_debug("imapc: /connection input");
 }
 
 static int imapc_connection_ssl_handshaked(void *context)
