Step 5: Write Code to Synchronise Time
The time synchronisation code ended up proving the most difficult. I had originally intended to use NTP (Network Time Protocol) to get the time, however I was unable to get this to work with the web server. Instead I ended up implementing the simpler DAYTIME protocol. This involves connecting to port 13 on a given sever, which then automatically responds with the time in a known format. The code parses this and updates its internal time accordingly.
Two possible server addresses are used. If one fails then the other is tried. These are queried on startup, and every hour afterwards. The DAYTIME server sites recommend you limit how often you query them, and may block your address if abused.