SOLVED: How to make a digitalOut high only while client connected via ethernet?

Hello all,
Any ideas why this code makes the LED go on as soon as you connect the ethernet cable?

Id like the LED to be on only while an ethernet client is connected.

void loop() {
 
  EthernetClient client = server.available();
  if (client) {
    if (!alreadyConnected){
      client.flush();
      Serial.println("Disconnected.");
      alreadyConnected = true;
    }
    
   while (!client.connected()){ //attempting to 
      digitalWrite(clientConnectLED, LOW); //turns off servos and laser while no client is connected.
      laser = false;
      digitalWrite(laserPin, LOW);
    }
    
    while (client.connected()){
      digitalWrite(clientConnectLED, HIGH); //powers on servos.
      delay(1);
   //rest of sketch:


}
}

vulture2600:
Any ideas why this code makes the LED go on as soon as you connect the ethernet cable?
Id like the LED to be on only while an ethernet client is connected.

No way to tell, without the rest of the sketch. We have no idea what your invisible functions are doing. We have no idea how you declared and/or initialized you variables, or what you coded in setup().

Ok, here is the current sketch. I have an LED on pin 2, just for testing purposes. I would like to hook up a solid state relay in front of the voltage regulator that is on the proto shield powering the servos. The idea is that while no clients are connected, the servos are powerless because they twitch every now and then and you can hear it.
The LED comes on when a client connects, but does not shut off when you disconnect.

/* attempting to program UNO with ethernet shield to accept commands from a client. 
copied code almost exactly from mouse_test

PWM pins remaining after ethernetshield are 3, 5, 6, and 9.

*/
#include <Servo.h>
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //mac address of eth shield
IPAddress ip (192,168,1,177); //permanent local IP address
IPAddress gateway (192,168,1,1); //router address
IPAddress subnet (255,255,255,0);
EthernetServer server(80); //using port 80

Servo servoX, servoY, servoA, servoB; //global variables:
int x1, y1, a1, b1;
const int servoPower = 2;
const int laserPin = 8;
const int buzzerPin = 7;
bool laser = false;
boolean alreadyConnected = false;

void setup() {
  servoX.attach(3);
  servoY.attach(5);
  servoA.attach(6);
  servoB.attach(9);
  servoX.write(90);
  servoY.write(90);
  
  pinMode(laserPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(servoPower, OUTPUT);
  digitalWrite(laserPin, LOW);
  
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  
  Serial.begin(19200);
  Serial.print("Web server address:");
  Serial.println(Ethernet.localIP());
  for(int i = 1; i <= 4; i++) { //blink LED 3 times.
    digitalWrite(servoPower, HIGH); //currently an LED is on servoPower pin, just as an indicator.
    delay(100);
    digitalWrite(servoPower, LOW);
    delay(100);
  }
}

void loop() {
  a1 = servoA.read(); //gets actual position of servoA/B to send back to processing.
  b1 = servoB.read();
  EthernetClient client = server.available();
  if (client) {
    if (!alreadyConnected){
      client.flush();
      Serial.println("Disconnected.");
      alreadyConnected = true;
    }
    
   while (!client.connected()){
      digitalWrite(servoPower, LOW); //turns off servos and laser while no client is connected. just an LED hooked up currently. power is always on.
      laser = false;
      digitalWrite(laserPin, LOW);
      servoX.write(90);
      servoY.write(90);
    }
    
    while (client.connected()){
      digitalWrite(servoPower, HIGH); //powers on servos.
      delay(1);
      static int v = 0;
      if (client.available()>0){
        char ch = client.read();
        switch(ch) { //read incomming characters from processing and handle them case by case.
          case '0'...'9': //0-9 are coordinates.
            v = v *10 + ch - '0';
            break;
            
          case 'x': //signifies end of x coordinate.
            x1 = map(v, 0, 180, 160, 0);
            servoX.write(x1);
            v = 0;
            break;
            
          case 'y': //end of y coordinate.
            y1 = map(v, 0, 180, 10, 170);
            servoY.write(y1);
            v = 0;
            break;
            
          case 'l': //processing sends laser on command while mouse left is held.
            digitalWrite(laserPin, HIGH);
            client.write("1");
            break;
            
           case 'k': //laser off command from processing.
             digitalWrite(laserPin, LOW);
             client.write("0");
             break;
          /*  
          case 'l': //turns laser on or off.
            laserOnOff();
            if (laser){
              client.write("1"); //tells processing laser is on.
            }
            else if (!laser){
              client.write("0"); //tells processing laser is off.
            }
            break;
            */
          case 'u': //moves servoA up and sends data back to processing to draw a position bar on screen if in range.
            a1 += 2;
            if (a1 >= 150){
              a1 = 150;
            }
            servoA.write(a1);
            if (a1 < 150){ //if servoA is in range, tell processing to move position bar.
              client.write("2");
            }
            break;
            
          case 'd':
            a1 -= 2;
            if (a1 <= 20){
              a1 = 20;
            }
            servoA.write(a1);
            if (a1 > 20){
              client.write("3");
            }
            break;
            
          case 'a':
            b1 -= 2;
            if (b1 <= 20){
              b1 = 20;
            }
            servoB.write(b1); //same for servoB:
            if (b1 > 20){
              client.write("4");
            }
            break;
           
          case 's':
            b1 += 2;
            if (b1 >= 160){
              b1 = 160;
            }
            servoB.write(b1);
            if (b1 < 160){
              client.write("5");
            }
            break;
          
          case 'b': //fires buzzer while mouse button is held.
            digitalWrite(buzzerPin, HIGH);
            break;
            
          case 'n':
            digitalWrite (buzzerPin, LOW);
            break;
        }   
      }
    } client.flush();
  }        
}

/*
void laserOnOff(){ //turns laser on/off.
  if (laser == false){
    digitalWrite (laserPin, HIGH);
    laser = true;
  }
  else if (laser == true){
    digitalWrite(laserPin, LOW);
    laser = false;
  }
}
*/

Fixed it. Seems that a digitalWrite(servoPower, LOW); after the while(client.connected(){ loop did the trick.

        }   
      }
    } 
   client.flush();
   digitalWrite(servoPower, LOW);
  }        
}