' NTP-like time request via UDP — simulate time synchronization
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL numServers  AS LONG
    LOCAL serverName(10) AS STRING
    LOCAL serverIP(10) AS STRING
    LOCAL rtt  AS LONG
    LOCAL offset  AS LONG
    LOCAL delay  AS LONG
    LOCAL localTime  AS DOUBLE
    LOCAL serverTime  AS DOUBLE
    LOCAL adjustedTime  AS DOUBLE
    LOCAL stratum  AS LONG
    LOCAL pollInterval  AS LONG
    LOCAL precision  AS DOUBLE
    LOCAL rootDelay  AS DOUBLE
    LOCAL leapIndicator  AS LONG
    LOCAL version  AS LONG
    LOCAL mode  AS LONG
    LOCAL origTimestamp  AS DOUBLE
    LOCAL recvTimestamp  AS DOUBLE
    LOCAL txTimestamp  AS DOUBLE
    LOCAL ntpPacket(48) AS LONG

    PRINT "=== NTP Time Client ==="
    PRINT

    numServers = 4
    serverName(0) = "pool.ntp.org                   "
    serverIP(0) = "162.159.200.1      "

    serverName(1) = "time.windows.com               "
    serverIP(1) = "131.107.13.100     "

    serverName(2) = "time.google.com                "
    serverIP(2) = "216.239.35.0       "

    serverName(3) = "time.cloudflare.com            "
    serverIP(3) = "162.159.200.123    "

    PRINT "NTP Servers:"
    FOR i = 0 TO numServers - 1
        PRINT "  " & TRIM$(serverName(i)) & " (" & TRIM$(serverIP(i)) & ")"
    NEXT i
    PRINT

    PRINT "--- NTP Query ---"
    PRINT "  Protocol: UDP port 123"
    PRINT "  Version: 4 (NTPv4)"
    PRINT "  Mode: 3 (Client)"
    PRINT

    leapIndicator = 0
    version = 4
    mode = 3
    PRINT "  LI: " & FORMAT$(leapIndicator) & " (no warning)"
    PRINT "  VN: " & FORMAT$(version)
    PRINT "  Mode: " & FORMAT$(mode) & " (client)"
    PRINT

    PRINT "--- Time Synchronization ---"
    localTime = 1000000.0
    serverTime = 1000010.0
    rtt = 50
    offset = (serverTime - localTime) / 2.0
    delay = rtt / 2

    PRINT "  Local time (T1): " & FORMAT$(localTime)
    PRINT "  Server time (T2): " & FORMAT$(serverTime)
    PRINT "  Round-trip time: " & FORMAT$(rtt) & " ms"
    PRINT "  Estimated offset: " & FORMAT$(offset) & " ms"
    PRINT "  One-way delay: " & FORMAT$(delay) & " ms"
    PRINT

    stratum = 2
    pollInterval = 6
    precision = 0.001
    rootDelay = 0.005

    PRINT "--- Server Stratum ---"
    PRINT "  Stratum: " & FORMAT$(stratum) & " (secondary server)"
    PRINT "  Poll interval: " & FORMAT$(pollInterval) & " (2^" & FORMAT$(pollInterval) & " = 64 s)"
    PRINT "  Precision: " & FORMAT$(precision) & " s"
    PRINT "  Root delay: " & FORMAT$(rootDelay) & " s"
    PRINT

    PRINT "--- Timestamps ---"
    PRINT "  Originate: " & FORMAT$(localTime)
    PRINT "  Receive: " & FORMAT$(serverTime)
    PRINT "  Transmit: " & FORMAT$(serverTime)
    PRINT

    adjustedTime = localTime + offset
    PRINT "  Adjusted local time: " & FORMAT$(adjustedTime)
    PRINT "  Offset applied: " & FORMAT$(offset) & " ms"
    PRINT

    PRINT "--- NTP Packet Format ---"
    PRINT "  Byte 0: LI(2) + VN(3) + Mode(3) = " & FORMAT$(leapIndicator * 64 + version * 8 + mode)
    PRINT "  Byte 1: Stratum"
    PRINT "  Byte 2: Poll interval"
    PRINT "  Byte 3: Precision"
    PRINT "  Bytes 4-7: Root delay"
    PRINT "  Bytes 8-11: Root dispersion"
    PRINT "  Bytes 12-15: Reference ID"
    PRINT "  Bytes 16-23: Reference timestamp"
    PRINT "  Bytes 24-31: Originate timestamp"
    PRINT "  Bytes 32-39: Receive timestamp"
    PRINT "  Bytes 40-47: Transmit timestamp"

    PRINT
    PRINT "NTP time client simulation complete."

    FUNCTION = 0
END FUNCTION
