<?php STREAM_CLIENT_ASYNC_CONNECT ?> opens a non-blocking socket (contradictory to the default blocking mode described in the manual page).
You will have to check for its status using <?php stream_select ?> followed by <?php socket_import_stream ?> and <?php socket_get_option($socket, SOL_SOCKET, SO_ERROR) ?>
You wouldn't normally do this, but when you do, you're more or less using glibc multisocket patterns.
Here's a minimal example:
<?php
$stream = stream_socket_client("tcp://127.0.0.1:7238", $errno, $errstr, flags: STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);
while (true) {
$w = [$stream];
$r = $e = [];
$changed = stream_select($r, $w, $e, 1, 0);
if (in_array($stream, $w)) {
break;
}
}
$socket = socket_import_stream($stream);
$so_error = socket_get_option($socket, SOL_SOCKET, SO_ERROR);
if ($so_error === 0) {
fwrite($stream, "Hello!\n");
} else {
echo "There's been an error: errno=$so_error\n";
}
?>