unsigned long getNtpTime()
{
	// We need to call the begin to reset the socket.
	// Because localClient.connect() may occupy the same socket.
	EthernetUDP theUDP;
	theUDP.begin(localPort);

	Serial.println("Sending time sync request to NTP server.");
	sendNTPpacket(timeServer); // send an NTP packet to a time server

	unsigned long startMillis = millis();
	int tryCounter = 1;
	while( millis() - startMillis < 1000)  // wait up to one second for the response
	{  // wait to see if a reply is available
		if ( theUDP.available() )
		{
			theUDP.readPacket(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer

			//the timestamp starts at byte 40 of the received packet and is four bytes,
			// or two words, long. First, esxtract the two words:
			unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
			unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
			// combine the four bytes (two words) into a long integer
			// this is NTP time (seconds since Jan 1 1900):
			unsigned long secsSince1900 = highWord << 16 | lowWord;
			// now convert NTP time into Arduino Time format:
			// Time starts on Jan 1 1970. In seconds, that's 2208988800:
			const unsigned long seventyYears = 2208988800UL;
			// subtract seventy years:
			unsigned long epoch = secsSince1900 - seventyYears;

			Serial.println("Time sync successfully.");

			return epoch;
		}

		Serial.print("No date data available. Try counter: .");
		Serial.println(tryCounter++);
		delay(100);
	}

	Serial.println("Time sync failed.");

	return 0;   // return 0 if unable to get the time
}