<?php
$sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
$path = __DIR__ . DIRECTORY_SEPARATOR . 'uds.sock';

if (file_exists($path))
	unlink($path);

if (!socket_bind($sock, $path))
{
	echo "Failed to bind Unix Socket", PHP_EOL;
	exit(1);
}

if (!socket_listen($sock, 5))
{
	echo "Failed to listen", PHP_EOL;
	exit(1);
}

$response = "Hello from PHP " . PHP_VERSION . " from " . PHP_OS;

while (true)
{
	$s = socket_accept($sock);
	$data = "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nDate: " . gmdate('D, d M Y H:i:s T') . "\r\nContent-Length: " . strlen($response) . "\r\n\r\n$response";
	echo "Received connection", PHP_EOL;
	socket_send($s, $data, strlen($data), 0);
	socket_close($s);
}