Index: res_xmpp.c
===================================================================
--- res_xmpp.c	(revision 397109)
+++ res_xmpp.c	(working copy)
@@ -2418,13 +2418,7 @@
 	}
 
 	if (!incoming) {
-		if (strlen(xmpp) == 1) {
-			if (option_debug > 2  && xmpp[0] == ' ') {
-				ast_verbose("\n<--- XMPP keep alive from '%s' --->\n", client->name);
-			}
-		} else {
-			ast_verbose("\n<--- XMPP sent to '%s' --->\n%s\n<------------->\n", client->name, xmpp);
-		}
+		ast_verbose("\n<--- XMPP sent to '%s' --->\n%s\n<------------->\n", client->name, xmpp);
 	} else {
 		ast_verbose("\n<--- XMPP received from '%s' --->\n%s\n<------------->\n", client->name, xmpp);
 	}
@@ -3109,6 +3103,40 @@
 	return res;
 }
 
+/*! \brief Helper function which sends a ping request to a server */
+static int xmpp_ping_request(struct ast_xmpp_client *client, const char *to, const char *from)
+{
+	iks *iq, *ping;
+	int res;
+	
+	ast_debug(2, "JABBER: Sending Keep-Alive Ping\n");
+
+	if (!(iq = iks_new("iq")) || !(ping = iks_new("ping"))) {
+		iks_delete(iq);
+		return -1;
+	}
+	
+	iks_insert_attrib(iq, "type", "get");
+	iks_insert_attrib(iq, "to", to);
+	iks_insert_attrib(iq, "from", from);
+	
+	ast_xmpp_client_lock(client);
+	iks_insert_attrib(iq, "id", client->mid);
+	ast_xmpp_increment_mid(client->mid);
+	ast_xmpp_client_unlock(client);
+	
+	iks_insert_attrib(ping, "xmlns", "urn:xmpp:ping");
+	iks_insert_node(iq, ping);
+	
+	res = ast_xmpp_client_send(client, iq);
+	
+	iks_delete(ping);
+	iks_delete(iq);
+
+
+	return res;
+}
+
 /*! \brief Internal function called when a presence message is received */
 static int xmpp_pak_presence(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak)
 {
@@ -3405,6 +3433,9 @@
 /*! \brief Internal function used to reconnect an XMPP client to its server */
 static int xmpp_client_reconnect(struct ast_xmpp_client *client)
 {
+	struct pollfd pfd = { .events = POLLIN };
+	struct timeval tv = { .tv_sec = 5, .tv_usec = 0 };
+	
 	RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
 	RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
 	int res = IKS_NET_NOCONN;
@@ -3427,6 +3458,11 @@
 	res = iks_connect_via(client->parser, S_OR(clientcfg->server, client->jid->server), clientcfg->port,
 			      ast_test_flag(&clientcfg->flags, XMPP_COMPONENT) ? clientcfg->user : client->jid->server);
 
+  	/* Set socket timeout options */
+  	pfd.fd = iks_fd(client->parser);
+  	setsockopt(pfd.fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
+  	/* End Set Socket Timeout Options */
+	
 	if (res == IKS_NET_NOCONN) {
 		ast_log(LOG_ERROR, "No XMPP connection available when trying to connect client '%s'\n", client->name);
 		return -1;
@@ -3445,6 +3481,7 @@
 static int xmpp_io_recv(struct ast_xmpp_client *client, char *buffer, size_t buf_len, int timeout)
 {
 	struct pollfd pfd = { .events = POLLIN };
+	
 	int len, res;
 
 #ifdef HAVE_OPENSSL
@@ -3458,6 +3495,7 @@
 		pfd.fd = iks_fd(client->parser);
 
 	res = ast_poll(&pfd, 1, timeout > 0 ? timeout * 1000 : -1);
+	
 	if (res > 0) {
 #ifdef HAVE_OPENSSL
 		if (xmpp_is_secure(client)) {
@@ -3511,6 +3549,14 @@
 		/* Log the message here, because iksemel's logHook is
 		   unaccessible */
 		xmpp_log_hook(client, buf, len, 1);
+		
+		if(buf[0] == ' ') {
+			ast_log(LOG_DEBUG, "JABBER: Detected Google Keep Alive. Sending out Ping request\n");
+			/*	I debate if we should just not send out the ping here
+				However then we will have socket read errors because 
+				the socket will timeout */
+			xmpp_ping_request(client, client->jid->server, client->jid->full);
+		}
 
 		/* let iksemel deal with the string length,
 		   and reset our buffer */
@@ -3544,6 +3590,7 @@
 
 	do {
 		if (client->state == XMPP_STATE_DISCONNECTING) {
+			ast_log(LOG_DEBUG, "JABBER: Disconnecting\n");
 			break;
 		}
 
@@ -3562,6 +3609,9 @@
 		 * old messages globally */
 		if (res == IKS_NET_EXPIRED) {
 			client->timeout--;
+			/* old 
+			ast_debug(2, "JABBER: Decreasing timeout to '%d'\n", client->timeout);
+			*/
 		}
 
 		if (res == IKS_HOOK) {
@@ -3573,7 +3623,7 @@
 			RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
 
 			if (cfg && cfg->clients && (clientcfg = xmpp_config_find(cfg->clients, client->name))) {
-				res = ast_test_flag(&clientcfg->flags, XMPP_KEEPALIVE) ? xmpp_client_send_raw_message(client, " ") : IKS_OK;
+				res = ast_test_flag(&clientcfg->flags, XMPP_KEEPALIVE) ? xmpp_ping_request(client, client->jid->server, client->jid->full) : IKS_OK;
 			} else {
 				res = IKS_OK;
 			}
@@ -3585,6 +3635,18 @@
 			}
 		} else if (res == IKS_NET_RWERR) {
 			ast_log(LOG_WARNING, "JABBER: socket read error\n");
+		} else if (res == IKS_NET_NOSOCK) {
+			ast_log(LOG_WARNING, "JABBER: No Socket\n");
+		} else if (res == IKS_NET_NOCONN) {
+			ast_log(LOG_WARNING, "JABBER: No Connection\n");
+		} else if (res == IKS_NET_NODNS) {
+			ast_log(LOG_WARNING, "JABBER: No DNS\n");
+		} else if (res == IKS_NET_NOTSUPP) {
+			ast_log(LOG_WARNING, "JABBER: Not Supported\n");
+		} else if (res == IKS_NET_DROPPED) {
+			ast_log(LOG_WARNING, "JABBER: Dropped?\n");
+		} else {
+			ast_debug(5, "JABBER: Unknown\n");
 		}
 
 	} while (1);
