Timeout support for SSL connections and better timeout handeling.
diff --git a/cython/heartbeat.pxi b/cython/heartbeat.pxi
index b48fb59..2f58909 100644
--- a/cython/heartbeat.pxi
+++ b/cython/heartbeat.pxi
@@ -9,6 +9,8 @@
         HEARTBEAT_E_PLIST_ERROR = -2
         HEARTBEAT_E_MUX_ERROR = -3
         HEARTBEAT_E_SSL_ERROR = -4
+        HEARTBEAT_E_NOT_ENOUGH_DATA = -5
+        HEARTBEAT_E_TIMEOUT = -6
         HEARTBEAT_E_UNKNOWN_ERROR = -256
 
     heartbeat_error_t heartbeat_client_new(idevice_t device, lockdownd_service_descriptor_t descriptor, heartbeat_client_t * client)
@@ -26,6 +28,8 @@
             HEARTBEAT_E_PLIST_ERROR: "Property list error",
             HEARTBEAT_E_MUX_ERROR: "MUX error",
             HEARTBEAT_E_SSL_ERROR: "SSL Error",
+            HEARTBEAT_E_NOT_ENOUGH_DATA: 'Not enough data',
+            HEARTBEAT_E_TIMEOUT: 'Connection timeout',
             HEARTBEAT_E_UNKNOWN_ERROR: "Unknown error"
         }
         BaseError.__init__(self, *args, **kwargs)
diff --git a/cython/imobiledevice.pyx b/cython/imobiledevice.pyx
index bc861b3..141f67c 100644
--- a/cython/imobiledevice.pyx
+++ b/cython/imobiledevice.pyx
@@ -38,8 +38,8 @@
         IDEVICE_E_UNKNOWN_ERROR = -2
         IDEVICE_E_NO_DEVICE = -3
         IDEVICE_E_NOT_ENOUGH_DATA = -4
-        IDEVICE_E_BAD_HEADER = -5
         IDEVICE_E_SSL_ERROR = -6
+        IDEVICE_E_TIMEOUT = -7
     ctypedef void (*idevice_event_cb_t) (const_idevice_event_t event, void *user_data)
     cdef extern idevice_error_t idevice_event_subscribe(idevice_event_cb_t callback, void *user_data)
     cdef extern idevice_error_t idevice_event_unsubscribe()
@@ -64,8 +64,8 @@
             IDEVICE_E_UNKNOWN_ERROR: 'Unknown error',
             IDEVICE_E_NO_DEVICE: 'No device',
             IDEVICE_E_NOT_ENOUGH_DATA: 'Not enough data',
-            IDEVICE_E_BAD_HEADER: 'Bad header',
-            IDEVICE_E_SSL_ERROR: 'SSL Error'
+            IDEVICE_E_SSL_ERROR: 'SSL Error',
+            IDEVICE_E_TIMEOUT: 'Connection timeout'
         }
         BaseError.__init__(self, *args, **kwargs)
 
diff --git a/cython/webinspector.pxi b/cython/webinspector.pxi
index 4622ef5..eb9fba1 100644
--- a/cython/webinspector.pxi
+++ b/cython/webinspector.pxi
@@ -26,6 +26,8 @@
             WEBINSPECTOR_E_PLIST_ERROR: "Property list error",
             WEBINSPECTOR_E_MUX_ERROR: "MUX error",
             WEBINSPECTOR_E_SSL_ERROR: "SSL Error",
+            WEBINSPECTOR_E_NOT_ENOUGH_DATA: 'Not enough data',
+            WEBINSPECTOR_E_TIMEOUT: 'Connection timeout',
             WEBINSPECTOR_E_UNKNOWN_ERROR: "Unknown error"
         }
         BaseError.__init__(self, *args, **kwargs)
diff --git a/include/libimobiledevice/heartbeat.h b/include/libimobiledevice/heartbeat.h
index 00734b5..1f5344a 100644
--- a/include/libimobiledevice/heartbeat.h
+++ b/include/libimobiledevice/heartbeat.h
@@ -34,12 +34,14 @@
 
 /** Error Codes */
 typedef enum {
-	HEARTBEAT_E_SUCCESS       =  0,
-	HEARTBEAT_E_INVALID_ARG   = -1,
-	HEARTBEAT_E_PLIST_ERROR   = -2,
-	HEARTBEAT_E_MUX_ERROR     = -3,
-	HEARTBEAT_E_SSL_ERROR     = -4,
-	HEARTBEAT_E_UNKNOWN_ERROR = -256
+	HEARTBEAT_E_SUCCESS         =  0,
+	HEARTBEAT_E_INVALID_ARG     = -1,
+	HEARTBEAT_E_PLIST_ERROR     = -2,
+	HEARTBEAT_E_MUX_ERROR       = -3,
+	HEARTBEAT_E_SSL_ERROR       = -4,
+	HEARTBEAT_E_NOT_ENOUGH_DATA = -5,
+	HEARTBEAT_E_TIMEOUT         = -6,
+	HEARTBEAT_E_UNKNOWN_ERROR   = -256
 } heartbeat_error_t;
 
 typedef struct heartbeat_client_private heartbeat_client_private;
@@ -118,6 +120,8 @@
  *
  * @return HEARTBEAT_E_SUCCESS on success,
  *      HEARTBEAT_E_INVALID_ARG when client or *plist is NULL,
+ *      HEARTBEAT_E_NOT_ENOUGH_DATA when not enough data
+ *      received, HEARTBEAT_E_TIMEOUT when the connection times out,
  *      HEARTBEAT_E_PLIST_ERROR when the received data cannot be
  *      converted to a plist, HEARTBEAT_E_MUX_ERROR when a
  *      communication error occurs, or HEARTBEAT_E_UNKNOWN_ERROR
diff --git a/include/libimobiledevice/libimobiledevice.h b/include/libimobiledevice/libimobiledevice.h
index 5ec1a6d..729bc89 100644
--- a/include/libimobiledevice/libimobiledevice.h
+++ b/include/libimobiledevice/libimobiledevice.h
@@ -41,8 +41,8 @@
 	IDEVICE_E_UNKNOWN_ERROR   = -2,
 	IDEVICE_E_NO_DEVICE       = -3,
 	IDEVICE_E_NOT_ENOUGH_DATA = -4,
-	IDEVICE_E_BAD_HEADER      = -5,
-	IDEVICE_E_SSL_ERROR       = -6
+	IDEVICE_E_SSL_ERROR       = -6,
+	IDEVICE_E_TIMEOUT         = -7
 } idevice_error_t;
 
 typedef struct idevice_private idevice_private;
diff --git a/include/libimobiledevice/property_list_service.h b/include/libimobiledevice/property_list_service.h
index 5d5b835..aca966d 100644
--- a/include/libimobiledevice/property_list_service.h
+++ b/include/libimobiledevice/property_list_service.h
@@ -38,6 +38,7 @@
 	PROPERTY_LIST_SERVICE_E_MUX_ERROR       = -3,
 	PROPERTY_LIST_SERVICE_E_SSL_ERROR       = -4,
 	PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT = -5,
+	PROPERTY_LIST_SERVICE_E_NOT_ENOUGH_DATA = -6,
 	PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR   = -256
 } property_list_service_error_t;
 
@@ -130,6 +131,8 @@
  *
  * @return PROPERTY_LIST_SERVICE_E_SUCCESS on success,
  *      PROPERTY_LIST_SERVICE_E_INVALID_ARG when client or *plist is NULL,
+ *      PROPERTY_LIST_SERVICE_E_NOT_ENOUGH_DATA when not enough data
+ *      received, PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT when the connection times out,
  *      PROPERTY_LIST_SERVICE_E_PLIST_ERROR when the received data cannot be
  *      converted to a plist, PROPERTY_LIST_SERVICE_E_MUX_ERROR when a
  *      communication error occurs, or PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR when
diff --git a/include/libimobiledevice/service.h b/include/libimobiledevice/service.h
index 13c5df1..5c43e29 100644
--- a/include/libimobiledevice/service.h
+++ b/include/libimobiledevice/service.h
@@ -37,6 +37,8 @@
 	SERVICE_E_MUX_ERROR           = -3,
 	SERVICE_E_SSL_ERROR           = -4,
 	SERVICE_E_START_SERVICE_ERROR = -5,
+	SERIVCE_E_NOT_ENOUGH_DATA     = -6,
+	SERVICE_E_TIMEOUT             = -7,
 	SERVICE_E_UNKNOWN_ERROR       = -256
 } service_error_t;
 
@@ -132,7 +134,9 @@
  *
  * @return SERVICE_E_SUCCESS on success,
  *      SERVICE_E_INVALID_ARG when one or more parameters are
- *      invalid, SERVICE_E_MUX_ERROR when a communication error
+ *      invalid, SERIVCE_E_NOT_ENOUGH_DATA when not enough data
+ *      received, SERVICE_E_TIMEOUT when the connection times out,
+ *      SERVICE_E_MUX_ERROR when a communication error
  *      occurs, or SERVICE_E_UNKNOWN_ERROR when an unspecified
  *      error occurs.
  */
@@ -146,7 +150,9 @@
  *
  * @return SERVICE_E_SUCCESS on success,
  *     SERVICE_E_INVALID_ARG if client or client->connection is
- *     NULL, SERVICE_E_SSL_ERROR when SSL could not be enabled,
+ *     NULL, SERIVCE_E_NOT_ENOUGH_DATA when not enough data
+ *     received, SERVICE_E_TIMEOUT when the connection times out,
+ *     SERVICE_E_SSL_ERROR when SSL could not be enabled,
  *     or SERVICE_E_UNKNOWN_ERROR otherwise.
  */
 service_error_t service_enable_ssl(service_client_t client);
diff --git a/include/libimobiledevice/webinspector.h b/include/libimobiledevice/webinspector.h
index d2a99c9..da0759c 100644
--- a/include/libimobiledevice/webinspector.h
+++ b/include/libimobiledevice/webinspector.h
@@ -35,12 +35,14 @@
 
 /** Error Codes */
 typedef enum {
-	WEBINSPECTOR_E_SUCCESS       =  0,
-	WEBINSPECTOR_E_INVALID_ARG   = -1,
-	WEBINSPECTOR_E_PLIST_ERROR   = -2,
-	WEBINSPECTOR_E_MUX_ERROR     = -3,
-	WEBINSPECTOR_E_SSL_ERROR     = -4,
-	WEBINSPECTOR_E_UNKNOWN_ERROR = -256
+	WEBINSPECTOR_E_SUCCESS         =  0,
+	WEBINSPECTOR_E_INVALID_ARG     = -1,
+	WEBINSPECTOR_E_PLIST_ERROR     = -2,
+	WEBINSPECTOR_E_MUX_ERROR       = -3,
+	WEBINSPECTOR_E_SSL_ERROR       = -4,
+	WEBINSPECTOR_E_RECEIVE_TIMEOUT = -5,
+	WEBINSPECTOR_E_NOT_ENOUGH_DATA = -6,
+	WEBINSPECTOR_E_UNKNOWN_ERROR   = -256
 } webinspector_error_t;
 
 typedef struct webinspector_client_private webinspector_client_private;
diff --git a/src/heartbeat.c b/src/heartbeat.c
index fe7e63a..9a527cc 100644
--- a/src/heartbeat.c
+++ b/src/heartbeat.c
@@ -52,6 +52,10 @@
 			return HEARTBEAT_E_MUX_ERROR;
 		case PROPERTY_LIST_SERVICE_E_SSL_ERROR:
 			return HEARTBEAT_E_SSL_ERROR;
+		case PROPERTY_LIST_SERVICE_E_NOT_ENOUGH_DATA:
+			return HEARTBEAT_E_NOT_ENOUGH_DATA;
+		case PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT:
+			return HEARTBEAT_E_TIMEOUT;
 		default:
 			break;
 	}
diff --git a/src/idevice.c b/src/idevice.c
index be29884..5d5c950 100644
--- a/src/idevice.c
+++ b/src/idevice.c
@@ -43,6 +43,7 @@
 
 #include "idevice.h"
 #include "common/userpref.h"
+#include "common/socket.h"
 #include "common/thread.h"
 #include "common/debug.h"
 
@@ -381,6 +382,24 @@
 	return internal_connection_send(connection, data, len, sent_bytes);
 }
 
+static idevice_error_t socket_recv_to_idevice_error(int conn_error, uint32_t len, uint32_t received)
+{
+	if (conn_error < 0) {
+		switch (conn_error) {
+			case -EAGAIN:
+				debug_info("ERROR: received partial data %d/%d (%s)", received, len, strerror(-conn_error));
+				return IDEVICE_E_NOT_ENOUGH_DATA;
+			case -ETIMEDOUT:
+				debug_info("ERROR: received timeout (%s)", strerror(-conn_error));
+				return IDEVICE_E_TIMEOUT;
+			default:
+				return IDEVICE_E_UNKNOWN_ERROR;
+		}
+	}
+
+	return IDEVICE_E_SUCCESS;
+}
+
 /**
  * Internally used function for receiving raw data over the given connection
  * using a timeout.
@@ -392,12 +411,14 @@
 	}
 
 	if (connection->type == CONNECTION_USBMUXD) {
-		int res = usbmuxd_recv_timeout((int)(long)connection->data, data, len, recv_bytes, timeout);
-		if (res < 0) {
-			debug_info("ERROR: usbmuxd_recv_timeout returned %d (%s)", res, strerror(errno));
-			return (res == -EAGAIN ? IDEVICE_E_NOT_ENOUGH_DATA : IDEVICE_E_UNKNOWN_ERROR);
+		int conn_error = usbmuxd_recv_timeout((int)(long)connection->data, data, len, recv_bytes, timeout);
+		idevice_error_t error = socket_recv_to_idevice_error(conn_error, len, *recv_bytes);
+
+		if (error == IDEVICE_E_UNKNOWN_ERROR) {
+			debug_info("ERROR: usbmuxd_recv_timeout returned %d (%s)", conn_error, strerror(-conn_error));
 		}
-		return IDEVICE_E_SUCCESS;
+
+		return error;
 	} else {
 		debug_info("Unknown connection type %d", connection->type);
 	}
@@ -406,13 +427,27 @@
 
 LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_receive_timeout(idevice_connection_t connection, char *data, uint32_t len, uint32_t *recv_bytes, unsigned int timeout)
 {
-	if (!connection || (connection->ssl_data && !connection->ssl_data->session)) {
+	if (!connection || (connection->ssl_data && !connection->ssl_data->session) || len == 0) {
 		return IDEVICE_E_INVALID_ARG;
 	}
 
 	if (connection->ssl_data) {
 		uint32_t received = 0;
+
 		while (received < len) {
+
+			int conn_error = socket_check_fd((int)(long)connection->data, FDM_READ, timeout);
+			idevice_error_t error = socket_recv_to_idevice_error(conn_error, len, received);
+
+			switch (error) {
+				case IDEVICE_E_SUCCESS:
+					break;
+				case IDEVICE_E_UNKNOWN_ERROR:
+					debug_info("ERROR: socket_check_fd returned %d (%s)", conn_error, strerror(-conn_error));
+				default:
+					return error;
+			}
+
 #ifdef HAVE_OPENSSL
 			int r = SSL_read(connection->ssl_data->session, (void*)((char*)(data+received)), (int)len-received);
 #else
@@ -424,13 +459,15 @@
 				break;
 			}
 		}
+
 		debug_info("SSL_read %d, received %d", len, received);
-		if (received > 0) {
-			*recv_bytes = received;
-			return IDEVICE_E_SUCCESS;
+		if (received < len) {
+			*recv_bytes = 0;
+			return IDEVICE_E_SSL_ERROR;
 		}
-		*recv_bytes = 0;
-		return IDEVICE_E_SSL_ERROR;
+		
+		*recv_bytes = received;
+		return IDEVICE_E_SUCCESS;
 	}
 	return internal_connection_receive_timeout(connection, data, len, recv_bytes, timeout);
 }
diff --git a/src/property_list_service.c b/src/property_list_service.c
index f411699..a6e3e24 100644
--- a/src/property_list_service.c
+++ b/src/property_list_service.c
@@ -48,6 +48,10 @@
 			return PROPERTY_LIST_SERVICE_E_MUX_ERROR;
 		case SERVICE_E_SSL_ERROR:
 			return PROPERTY_LIST_SERVICE_E_SSL_ERROR;
+		case SERIVCE_E_NOT_ENOUGH_DATA:
+			return PROPERTY_LIST_SERVICE_E_NOT_ENOUGH_DATA;
+		case SERVICE_E_TIMEOUT:
+			return PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT;
 		default:
 			break;
 	}
@@ -108,7 +112,7 @@
 	char *content = NULL;
 	uint32_t length = 0;
 	uint32_t nlen = 0;
-	int bytes = 0;
+	uint32_t bytes = 0;
 
 	if (!client || (client && !client->parent) || !plist) {
 		return PROPERTY_LIST_SERVICE_E_INVALID_ARG;
@@ -126,13 +130,13 @@
 
 	nlen = htobe32(length);
 	debug_info("sending %d bytes", length);
-	service_send(client->parent, (const char*)&nlen, sizeof(nlen), (uint32_t*)&bytes);
+	service_send(client->parent, (const char*)&nlen, sizeof(nlen), &bytes);
 	if (bytes == sizeof(nlen)) {
-		service_send(client->parent, content, length, (uint32_t*)&bytes);
+		service_send(client->parent, content, length, &bytes);
 		if (bytes > 0) {
 			debug_info("sent %d bytes", bytes);
 			debug_plist(plist);
-			if ((uint32_t)bytes == length) {
+			if (bytes == length) {
 				res = PROPERTY_LIST_SERVICE_E_SUCCESS;
 			} else {
 				debug_info("ERROR: Could not send all data (%d of %d)!", bytes, length);
@@ -145,7 +149,6 @@
 	}
 
 	free(content);
-
 	return res;
 }
 
@@ -170,6 +173,8 @@
  *
  * @return PROPERTY_LIST_SERVICE_E_SUCCESS on success,
  *      PROPERTY_LIST_SERVICE_E_INVALID_ARG when client or *plist is NULL,
+ *      PROPERTY_LIST_SERVICE_E_NOT_ENOUGH_DATA when not enough data
+ *      received, PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT when the connection times out,
  *      PROPERTY_LIST_SERVICE_E_PLIST_ERROR when the received data cannot be
  *      converted to a plist, PROPERTY_LIST_SERVICE_E_MUX_ERROR when a
  *      communication error occurs, or PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR
@@ -187,65 +192,64 @@
 
 	*plist = NULL;
 	service_error_t serr = service_receive_with_timeout(client->parent, (char*)&pktlen, sizeof(pktlen), &bytes, timeout);
-	if ((serr == SERVICE_E_SUCCESS) && (bytes == 0)) {
-		return PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT;
-	}
-	debug_info("initial read=%i", bytes);
-	if (bytes < 4) {
+	if (serr != SERVICE_E_SUCCESS) {
 		debug_info("initial read failed!");
-		return PROPERTY_LIST_SERVICE_E_MUX_ERROR;
-	} else {
-		uint32_t curlen = 0;
-		char *content = NULL;
+		return service_to_property_list_service_error(serr);
+	}
 
-		pktlen = be32toh(pktlen);
-		debug_info("%d bytes following", pktlen);
-		content = (char*)malloc(pktlen);
-		if (!content) {
-			debug_info("out of memory when allocating %d bytes", pktlen);
-			return PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR;
-		}
+	debug_info("initial read=%i", bytes);
 
-		while (curlen < pktlen) {
-			service_receive(client->parent, content+curlen, pktlen-curlen, &bytes);
-			if (bytes <= 0) {
-				res = PROPERTY_LIST_SERVICE_E_MUX_ERROR;
-				break;
-			}
-			debug_info("received %d bytes", bytes);
-			curlen += bytes;
+	uint32_t curlen = 0;
+	char *content = NULL;
+
+	pktlen = be32toh(pktlen);
+	debug_info("%d bytes following", pktlen);
+	content = (char*)malloc(pktlen);
+	if (!content) {
+		debug_info("out of memory when allocating %d bytes", pktlen);
+		return PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR;
+	}
+
+	while (curlen < pktlen) {
+		serr = service_receive(client->parent, content+curlen, pktlen-curlen, &bytes);
+		if (serr != SERVICE_E_SUCCESS) {
+			res = service_to_property_list_service_error(serr);
+			break;
 		}
-		if (curlen < pktlen) {
-			debug_info("received incomplete packet (%d of %d bytes)", curlen, pktlen);
-			if (curlen > 0) {
-				debug_info("incomplete packet following:");
-				debug_buffer(content, curlen);
-			}
-			free(content);
-			return res;
-		}
-		if ((pktlen > 8) && !memcmp(content, "bplist00", 8)) {
-			plist_from_bin(content, pktlen, plist);
-		} else if ((pktlen > 5) && !memcmp(content, "<?xml", 5)) {
-			/* iOS 4.3+ hack: plist data might contain invalid characters, thus we convert those to spaces */
-			for (bytes = 0; bytes < pktlen-1; bytes++) {
-				if ((content[bytes] >= 0) && (content[bytes] < 0x20) && (content[bytes] != 0x09) && (content[bytes] != 0x0a) && (content[bytes] != 0x0d))
-					content[bytes] = 0x20;
-			}
-			plist_from_xml(content, pktlen, plist);
-		} else {
-			debug_info("WARNING: received unexpected non-plist content");
-			debug_buffer(content, pktlen);
-		}
-		if (*plist) {
-			debug_plist(*plist);
-			res = PROPERTY_LIST_SERVICE_E_SUCCESS;
-		} else {
-			res = PROPERTY_LIST_SERVICE_E_PLIST_ERROR;
+		debug_info("received %d bytes", bytes);
+		curlen += bytes;
+	}
+	if (curlen < pktlen) {
+		debug_info("received incomplete packet (%d of %d bytes)", curlen, pktlen);
+		if (curlen > 0) {
+			debug_info("incomplete packet following:");
+			debug_buffer(content, curlen);
 		}
 		free(content);
-		content = NULL;
+		return res;
 	}
+	if ((pktlen > 8) && !memcmp(content, "bplist00", 8)) {
+		plist_from_bin(content, pktlen, plist);
+	} else if ((pktlen > 5) && !memcmp(content, "<?xml", 5)) {
+		/* iOS 4.3+ hack: plist data might contain invalid characters, thus we convert those to spaces */
+		for (bytes = 0; bytes < pktlen-1; bytes++) {
+			if ((content[bytes] >= 0) && (content[bytes] < 0x20) && (content[bytes] != 0x09) && (content[bytes] != 0x0a) && (content[bytes] != 0x0d))
+				content[bytes] = 0x20;
+		}
+		plist_from_xml(content, pktlen, plist);
+	} else {
+		debug_info("WARNING: received unexpected non-plist content");
+		debug_buffer(content, pktlen);
+	}
+	if (*plist) {
+		debug_plist(*plist);
+		res = PROPERTY_LIST_SERVICE_E_SUCCESS;
+	} else {
+		res = PROPERTY_LIST_SERVICE_E_PLIST_ERROR;
+	}
+	free(content);
+	content = NULL;
+	
 	return res;
 }
 
diff --git a/src/service.c b/src/service.c
index 2dc42b2..57d987c 100644
--- a/src/service.c
+++ b/src/service.c
@@ -46,6 +46,10 @@
 			return SERVICE_E_INVALID_ARG;
 		case IDEVICE_E_SSL_ERROR:
 			return SERVICE_E_SSL_ERROR;
+		case IDEVICE_E_NOT_ENOUGH_DATA:
+			return SERIVCE_E_NOT_ENOUGH_DATA;
+		case IDEVICE_E_TIMEOUT:
+			return SERVICE_E_TIMEOUT;
 		default:
 			break;
 	}
@@ -159,8 +163,9 @@
 	}
 
 	res = idevice_to_service_error(idevice_connection_receive_timeout(client->connection, data, size, (uint32_t*)&bytes, timeout));
-	if (bytes <= 0) {
+	if (res != SERVICE_E_SUCCESS) {
 		debug_info("could not read data");
+		return res;
 	}
 	if (received) {
 		*received = (uint32_t)bytes;
diff --git a/src/webinspector.c b/src/webinspector.c
index c81f4c7..3360597 100644
--- a/src/webinspector.c
+++ b/src/webinspector.c
@@ -52,6 +52,10 @@
 			return WEBINSPECTOR_E_MUX_ERROR;
 		case PROPERTY_LIST_SERVICE_E_SSL_ERROR:
 			return WEBINSPECTOR_E_SSL_ERROR;
+		case PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT:
+			return WEBINSPECTOR_E_RECEIVE_TIMEOUT;
+		case PROPERTY_LIST_SERVICE_E_NOT_ENOUGH_DATA:
+			return WEBINSPECTOR_E_NOT_ENOUGH_DATA;
 		default:
 			break;
 	}
diff --git a/tools/idevicecrashreport.c b/tools/idevicecrashreport.c
index e05d506..6918d6b 100644
--- a/tools/idevicecrashreport.c
+++ b/tools/idevicecrashreport.c
@@ -411,7 +411,7 @@
 	while ((strncmp(ping, "ping", 4) != 0) && (attempts < 10)) {
 		uint32_t bytes = 0;
 		device_error = idevice_connection_receive_timeout(connection, ping, 4, &bytes, 2000);
-		if ((bytes == 0) && (device_error == IDEVICE_E_SUCCESS)) {
+		if (device_error != IDEVICE_E_SUCCESS) {
 			attempts++;
 			continue;
 		} else if (device_error < 0) {