Socket_read problem in php

I wrote a test socket link test server-side php script.
after being executed in linux, the content returned by the server can be returned. But later, it is judged that the code that exits in more than 3 seconds will not be executed, and the php script process will be there all the time. How should I deal with it? I"m in a bit of a hurry, and the question may be a little unclear. Sorry.)

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
        exit;
    }
    $result = socket_connect($socket, $host, $port);
    if ($result === false) {
        exit;
    }
    socket_write($socket, $str, strlen($str));//socket.
    $time = time();
    while($king=socket_read($socket,1024)){
        echo $king;
        if((time()-$time)>3){
            echo "";
            socket_write($socket, $str, strlen($exit));
            exit;
        }
    }
Jun.10,2021

this is not read EOF, socket_read is blocked

Menu