Photon lock up in TCPClient write()

I’m debugging an application that make the photon send data to a server using TCP. When my server is down, the photon is stuck in write(). It does not return any value, the photon change to another mode, and never recover from that mode. Does any one encounter the same problem? Please help. I appreciate that.
By the way, photon is in Automatic mode, no Thread

Seeing some code would be helpful here.

Checking client.connected() and/or client.status() before attempting to client.write() would be a starting point.

I did check client.status() and client.connected() before write. Here is my code:

                if (client.connected() && client.status()){
                    b = client.write(packet, PACKET_S);    // Send 1204 byte buffer
                    Serial.printlnf("Client status %d",client.status()); // Never reach here when server is down 
                    Serial.println("The server is dead");
                    while(serverConnect()); //reconnection to server
                    b = -1;
            }while((b!=PACKET_S) || (b == -1));