#! /usr/bin/perl
#
use IO::Socket;

unless (defined($ARGV[0]))
  {
  print "Usage: $0 <webserver hostname>\n";
  exit(1);
  }

$datecommand = 'date --set=';  # 'set system time' command
$DEBUG = 1;

$request="HEAD /";
$connect_host = $ARGV[0];
$request .= " HTTP/1.1\r\nHost: $ARGV[0]\r\nUser-Agent:tralala\r\nPragma: no-cache\r\nCache-Control: Max-age=0\r\n\r\n";

$socket = IO::Socket::INET->new (
        Proto           => 'tcp',
        PeerAddr        => $connect_host,
        PeerPort        => '80');

# Send HEAD requests
print "Request:\n$request" if ($DEBUG);
print $socket $request;
while (defined ( $res = <$socket>) )
  {
  print "Answer: $res" if($DEBUG);
  if ($res =~ /Date: /)
    {
    $res =~ s/Date: //;
    print "Setting time... $res\n" if($DEBUG);
    system($datecommand . "'$res'");
    close($socket);
    }
  }
