ESP32 Wifi "connect" fails to return control

I'm working with an ESP32 and using WifiSecureClient. Normally everything works fine, but sometimes the first ".connect" never returns control.

WifiSecureClient client;

void setup () {

  // establish wifi connection - it works fine


void loop() {

  // unrelated code

The sketch initially connects to wifi just fine and I can read the IP address and other information about the connection. The next thing it does (related to wifi) is to run the following statement:

  if (!client.connect) { 
    // ....... more ..... 



99 out of 100 times, when the sketch starts, everything is fine. But 1 out of 100, the sketch never returns control from the first call to client.connect - it just hangs there.

When this happens, it is always on the very first call to client.connect and I have to restart the project. If the very first call works fine, the project will continue to run fine (with repeated calls to client. connect) forever.

If this post doesn't really belong under the Arduino forum, perhaps someone can suggest where to ask the question.