BearSSL WiFiClientSecure sometimes freeze at client.connect()

Hi,

I use WiFiClientSecure client.connect() to call to "script.google.com".
Sometimes, it can be connected successfully.
However, sometimes it freezed.

Here is the code:

const char* host = "script.google.com";
const int httpsPort = 443;
WiFiClientSecure client;
...
void loop()
{
  client.setInsecure();
  if (!client.connect(host, httpsPort)) { // <--- if it failed, it freezed at this line ( never print either "connection failed" or "ok" )
    Serial.println("connection failed");
    return;
  }
    Serial.println("ok");
}

The debug message when succeed

14:40:02.516 -> :ur 1
14:40:02.516 -> :del
14:40:02.516 -> :ref 1
:wr 222 0
14:40:02.586 -> :wrc 222 222 0
:ack 222
14:40:02.620 -> :rn 536
14:40:02.620 -> :rd 5, 536, 0
14:40:02.620 -> :rdi 536, 5
14:40:02.620 -> :rch 536, 536
14:40:02.662 -> :rd 87, 1072, 5
14:40:02.662 -> :rdi 531, 87
14:40:02.662 -> :rd 5, 1072, 92
14:40:02.662 -> :rdi 444, 5
14:40:02.662 -> :rd 975, 1072, 97
14:40:02.662 -> :rdi 439, 439
14:40:02.662 -> :c 439, 536, 1072
14:40:02.662 -> :rdi 536, 536
14:40:02.662 -> :c0 536, 536 
:rn 1072
14:40:02.696 -> :rd 1072, 1072, 0
14:40:02.696 -> :rdi 536, 536
14:40:02.696 -> :c 536, 536, 1072
14:40:02.696 -> :rdi 536, 536
14:40:02.696 -> :c0 536, 536
14:40:02.696 -> :rn 536
14:40:02.737 -> :rch 536, 536
14:40:02.737 -> :rd 1072, 1072, 0
14:40:02.737 -> :rdi 536, 536
14:40:02.737 -> :c 536, 536, 1072
14:40:02.737 -> :rdi 536, 536
14:40:02.737 -> :c0 536, 536
14:40:02.737 -> :rn 226
14:40:02.737 -> :rd 98, 226, 0
14:40:02.737 -> :rdi 226, 98
14:40:02.737 -> :rd 5, 226, 98
14:40:02.737 -> :rdi 128, 5
14:40:02.737 -> :rd 114, 226, 103
14:40:02.737 -> :rdi 123, 114
:rd 5, 226, 217
14:40:03.270 -> :rdi 9, 5
14:40:03.270 -> :rd 4, 226, 222
14:40:03.270 -> :rdi 4, 4
14:40:03.270 -> :c0 4, 226
:wr 42 0
14:40:03.536 -> :wrc 42 42 0
14:40:03.536 -> :wr 6 0
14:40:03.536 -> :wrc 6 6 0
14:40:03.536 -> :wr 37 0
14:40:03.536 -> :wrc 37 37 0
:ack 42
:ack 43
14:40:03.614 -> :rn 43
14:40:03.614 -> :rd 5, 43, 0
14:40:03.614 -> :rdi 43, 5
14:40:03.614 -> :rd 1, 43, 5
14:40:03.614 -> :rdi 38, 1
14:40:03.614 -> :rd 5, 43, 6
14:40:03.614 -> :rdi 37, 5
14:40:03.614 -> :rd 32, 43, 11
14:40:03.614 -> :rdi 32, 32
14:40:03.614 -> :c0 32, 43

The debug message when fail

14:40:07.720 -> :ur 1
14:40:07.720 -> :del
14:40:07.720 -> :ref 1
:wr 222 0
14:40:07.798 -> :wrc 222 222 0
:ack 222
14:40:07.839 -> :rn 536
14:40:07.839 -> :rd 5, 536, 0
14:40:07.839 -> :rdi 536, 5
14:40:07.839 -> :rd 87, 536, 5
14:40:07.839 -> :rdi 531, 87
14:40:07.839 -> :rd 5, 536, 92
14:40:07.839 -> :rdi 444, 5
14:40:07.839 -> :rd 439, 536, 97
14:40:07.839 -> :rdi 439, 439
14:40:07.839 -> :c0 439, 536

The ":c0 439, 536" is the last message I got from the debug console.
After that, there is no more debug message.

The hang is varied.
Sometimes, it is just :ack 222 and it is hang.
Sometimes, it is :c0 536, 536.
etc.

15:23:59.243 -> :ur 1
15:23:59.243 -> :del
15:23:59.243 -> :ref 1
15:23:59.243 -> :wr 222 0
15:23:59.243 -> :wrc 222 222 0
15:23:59.243 -> :ack 222
15:26:28.548 -> :ur 1
15:26:28.548 -> :del
15:26:28.548 -> :ref 1
15:26:28.548 -> :wr 222 0
15:26:28.548 -> :wrc 222 222 0
15:26:28.548 -> :ack 222
15:26:28.548 -> :rn 536
15:26:28.548 -> :rd 5, 536, 0
15:26:28.548 -> :rdi 536, 5
15:26:28.548 -> :rd 87, 536, 5
15:26:28.548 -> :rdi 531, 87
15:26:28.548 -> :rd 5, 536, 92
15:26:28.548 -> :rdi 444, 5
15:26:28.548 -> :rd 439, 536, 97
15:26:28.548 -> :rdi 439, 439
15:26:28.548 -> :c0 439, 536
15:26:28.548 -> :rn 536
15:26:28.548 -> :rd 536, 536, 0
15:26:28.548 -> :rdi 536, 536
15:26:28.548 -> :c0 536, 536
15:26:28.548 -> :rn 536
15:26:28.548 -> :rd 536, 536, 0
15:26:28.548 -> :rdi 536, 536
15:26:28.548 -> :c0 536, 536

Could you guys please help ?

Post all of your code, as an attachment if it's too long to include inline.

wildbill:
Post all of your code, as an attachment if it's too long to include inline.

Here it is.
https://raw.githubusercontent.com/dsin/Upload-Sensor-Data-to-Google-Sheets-from-NodeMCU/master/arduino_code.txt

NOTE : It is not happened every time.
Sometimes, we can connect to the script.google.com.
However, if it could not connect, then it is not.

SOLVED !

by putting client.setTimeout(5000) before client.connect()