Arduino Client Disconnected After the Motor Works

Hello, in my following code, I am trying to connect to some server and then run a motor. I succeed to connect to the server and run the motor, but after the second time the motor is working, the client is disconnected and I do not know where is the problem.

#include 
#include 


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

int motorPin = 3;

IPAddress ip(192, 168, 0, 9);

EthernetClient client;

void setup()
{
  Serial.begin(9600);
  pinMode(motorPin, OUTPUT);
  while (!Serial) 
  {
    ;
  }

  if (Ethernet.begin(mac) == 0) 
  {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }

  delay(1000);
  Serial.println("connecting...");

  if (client.connect("192.168.1.104", 1688))
  {
    Serial.println("connected");
  }
  else 
  {
    Serial.println("connection failed");
  }
}

void loop()
{
  char c = client.read();
  if(c != -1)     //available chrs to read from server
  {
    if (c != '\0')
    {
      str += c;
    }
    else
    {
      analogWrite(motorPin, 255);
      delay(6000);
      analogWrite(motorPin, LOW);
    }
  }
}

Thank for the helpers!

  char c = client.read();
  if(c != -1)     //available chrs to read from server

You MUST check that there is a client.connected() before you try to read. You should use client.available() to see if there is any data to read.

    if (c != '\0')

The NULL that defines the end of a string is NEVER transmitted. So, this test will always evaluate to true. Therefore, it is useless.

Of course, that means the whole else block is useless, too.