datasort state =
| uninit
| listening
| active
| closed
absvtype socket (state)
typedef port = int
typedef addr = string
extern fun create (): socket (uninit)
extern fun listen (!socket (uninit) >> socket (listening), port): void
extern fun accept (!socket (listening) >> socket (active)): void
extern fun connect (!socket (uninit) >> socket (active), addr): void
extern fun send (!socket (active), int): void
extern fun recv (!socket (active)): int
extern fun close {s:state} (socket (s)): void
implement main0 () = let
val sock = create ()
val _ = listen (sock, 8080)
val _ = accept (sock) // block until someone connects
val _ = send (sock, 1)
val x = recv (sock)
in
close sock
end