From 8abc16f501816307d45304f090c56fccd7b09a21 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Mon, 8 Aug 2016 16:39:44 -0500
Subject: [PATCH] FS-8761

---
 libs/sofia-sip/.update                        |  2 +-
 .../libsofia-sip-ua/tport/tport_type_ws.c     | 30 ++++++++++++++-----
 .../libsofia-sip-ua/tport/tport_ws.h          |  2 +-
 3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/libs/sofia-sip/.update b/libs/sofia-sip/.update
index b824aceab0..0ef1bfcdf6 100644
--- a/libs/sofia-sip/.update
+++ b/libs/sofia-sip/.update
@@ -1 +1 @@
-Sat Aug  6 11:48:53 CDT 2016
+Mon Aug  8 16:39:35 CDT 2016
diff --git a/libs/sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c b/libs/sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c
index 1390689513..6b525581f1 100644
--- a/libs/sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c
+++ b/libs/sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c
@@ -494,6 +494,8 @@ int tport_ws_init_secondary(tport_t *self, int socket, int accepted,
 	  return *return_reason = "WS_INIT", -1;
   }
 
+  wstp->connected = time(NULL);
+
   wstp->ws_initialized = 1;
   self->tp_pre_framed = 1;
   
@@ -594,19 +596,31 @@ int tport_ws_next_timer(tport_t *self,
 			 char const **return_why)
 {
 	tport_ws_t *wstp = (tport_ws_t *)self;
+	int ll = establish_logical_layer(&wstp->ws);
+	int punt = 0;
 
-	if (establish_logical_layer(&wstp->ws) < 0) {
-		if (wstp->tos++ == 1) {
-			tport_close(self);
-			SU_DEBUG_7(("%s(%p): %s to " TPN_FORMAT "%s\n",
-						__func__, (void *)self,
-						"timeout establishing connection\n", TPN_ARGS(self->tp_name), ""));
-			return -1;
+	if (ll == -1) {
+		punt = 1;
+	} else if (ll < 0) {
+		time_t now = time(NULL);
+		if (now - wstp->connected > 5) {
+			punt = 2;
 		}
 	} else {
-		self->tp_params->tpp_keepalive = 30000;
+		self->tp_params->tpp_keepalive = 0;
 	}
 
+	if (punt) {
+		tport_close(self);
+
+		SU_DEBUG_7(("%s(%p): %s to " TPN_FORMAT "%s\n",
+					__func__, (void *)self,
+					(punt == 2 ? "Timeout establishing SSL" : "Error establishing SSL"), TPN_ARGS(self->tp_name), ""));
+
+		return -1;
+	}
+
+
   return
     tport_next_recv_timeout(self, return_target, return_why) |
     tport_next_keepalive(self, return_target, return_why);
diff --git a/libs/sofia-sip/libsofia-sip-ua/tport/tport_ws.h b/libs/sofia-sip/libsofia-sip-ua/tport/tport_ws.h
index 4e2e0f474d..d43e02e2ca 100644
--- a/libs/sofia-sip/libsofia-sip-ua/tport/tport_ws.h
+++ b/libs/sofia-sip/libsofia-sip-ua/tport/tport_ws.h
@@ -59,7 +59,7 @@ typedef struct tport_ws_s {
   char     wstp_buffer[65536];
   size_t   wstp_buflen;
   SU_S8_T  ws_initialized;
-  unsigned char tos;
+  time_t connected;
   unsigned ws_secure:1;
   unsigned:0;
 } tport_ws_t;