Socket read and write in the same time

i’ve started creating a program in php cli that conects to a server from where i read some datas and i must send back also soma data

i will try to make a schematic:

socket_conect…

while (true)
{
$x=socket_read

echo $x;

$y=mysql_query(“select * …”);

socket_write($y[0]);
}

something like this works my program, the problem is with this:
the program reads some data and then sends back, but if there is no data to read he waits until there is something to read and he doesnt send nothing in this waiting time

i can’t find anything to rezolve this problem,
please help