replace socket magic numbers with posix constants, fix race where socket state was set after closing FD