Long life http connection, Connection:keep-alive not working...

I am trying to get my Arduino to receive commands from a website in real time.
I am using the Arduino WifiShield with the WebServer example that I modified to receive data from clients.

Connecting and disconnecting is slow (1~2 seconds) and I am trying to keep the connection open using something like:

client.println("Connection: keep-alive");
client.println("Keep-Alive: timeout=30, max=100");

in the HTTP header. This by itself doesn't work and the Arduino continues to disconnect from the client. So I tried removing the client.stop(); command, but then the page wouldn't load at all. I'm thinking, should I try tinkering with the

if (c == '\n') {
// you're starting a new line
} 
else if (c != '\r') {
// you've gotten a character on the current line
}

lines to make the Arduino understand that it should not disconnect yet. Here is the main part of my loop function. Any thoughts?

void loop() {
	
	// listen for incoming clients
	WiFiClient client = server.available();
	
	if (client) {
			Serial.println("client exists");

		// an http request ends with a blank line
		boolean currentLineIsBlank = true;
		
		while (client.connected()) {
			

				if (client.available()) {
						

						char c = client.read();
																	


						// if you've gotten to the end of the line (received a newline
						// character) and the line is blank, the http request has ended,
						// so you can send a reply
						if (c == '\n' && currentLineIsBlank) {
						// send a standard http response header
								
								
								String POST = "";
								
								while(client.available()){

										c = client.read();
										// save the variables somewhere
										POST += c;
								}
								 

								Serial.println("sending data to client");

								//HEADER
								client.println("HTTP/1.1 200 OK");
								client.println("Content-Type: text/html");
								client.println("Connection: keep-alive");
								client.println("Keep-Alive: timeout=30, max=100");
								client.println();

								//HTML 
								client.println("<!DOCTYPE html>");
								client.println("<html>");
								client.println("<head>");
								client.println("<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>");
								client.println("<script src='https://rawgit.com/joestox/moll_e/master/script.js'></script>");
								client.println("</head>");
								client.println("</html>");
                                                                                      delay(1000);
								
								break;
						}

						if (c == '\n') {
								// you're starting a new line
								currentLineIsBlank = true;
						} 
						else if (c != '\r') {
								// you've gotten a character on the current line
								currentLineIsBlank = false;
						}
				} // if client is available

		} //while client is connected
		// give the web browser time to receive the data
		delay(100);
		client.stop();
		Serial.println("client disonnected");

	} //if client

}

client.stop() will close the connection. Last I checked, if you want to use a persistent connection, you must send a Content-Length parameter in the response header so the client will know when to quit listening for more data from the server. Otherwise, the client.stop() call will close the connection, letting the client know it is finished sending.

Thanks for the reply. But in my case, I'm sending data from the client to the server. Should I still include something like:

client.println("Content-Length: 400");

in the HTTP headers in my code?

My bad. I saw this and assumed it was server code:

	WiFiClient client = server.available();

That is how the server determines there is a request pending from a client.

If you are sending a GET request, the Content-Length is not necessary. Only for a POST request. The server will return an Content-Length parameter to let you know how many characters you should read. Normally, the server will send packets until it is finished, then close the connection.