Restore the status of client.connected ()

Hello,
I use the Mega2560 in conjunction with the ENC28J60 (ethernet shield). Mam running TCP client soceke. Successfully connect the Python sooty server to the server and send the data from the server and receive it to the client. In the while () cycle in the data server. In this cycle, I also check the if (client.conecteed ()) state but when I turn off the server and the program is in cyke while the return from client.connected () is true. and when the server is turned off, the status is still true. Can you help me find some solution?

while((size = client.available()) > 0)
            {
              uint8_t* msg = (uint8_t*)malloc(size);
              size = client.read(msg,size);
              Serial.write(msg,size);
              Serial.println(" ");
              free(msg);
              if (!client.connected())
                {
                  Serial.println("Connection LOST !!!");
                  client.stop();
                }
            }
while((size = client.available()) > 0)
{
  ...
  if (!client.connected())
    ...
}

Surely if the client is not connected, then client.available() cannot return data ?
so your program never gets to the if(!client.connected())

Yours,
TonyWilk

BUt this IF is only for checking state of connectivity while is cykle WHILE (reading) run....