PHP socket_recv换行问题

I have the following that reads from a buffer and send a message to the socket.

Depending the client (classic, telnet, windows, linux , putty, etc) it behaves differently. It send the line after the newline, other times, character by character.

I need to add a control so it only sends the message after a newline is detected.

Any ideas?

foreach ($this->changed as $key => $socket) {
    $buffer = null;
    while (socket_recv($socket, $buffer, 1024, 0) >= 1) {
        $this->sendMessage(trim($buffer) . PHP_EOL);
        unset($this->changed[$key]);
        break;
    }
}

Here is the sendMessage function

function sendMessage($msg)
{
    foreach($this->clients as $client)
    {
        @socket_write($client,$msg,strlen($msg));
    }
    return true;
}
foreach ($this->changed as $key => $socket) 
{
    $buffer = null;
    $tempbuffer=null;
    while(socket_recv($socket, $tempbuffer, 1024, 0) >= 1) {
        if(strpos($tempbuffer,'
')==false)
            $tempbuffer.=$buffer;
    }
    if($buffer==null)
       $buffer=$tempbuffer;

    $this->sendMessage(trim($buffer) . PHP_EOL);
    unset($this->changed[$key]);

    break;

}