$log = fopen('log.html', 'a'); while($data = fgets($socket, 128)) { echo nl2br($data); fwrite($log, nl2br($data)); flush(); } fclose($log);