Reading rompt protocols


I’m trying to implement a function that reads from a socket ( TCP/IP
based protocol with prompt ).

Server sends:

Welcoming you\r\n
Please login\r\n

I need this function to be a wrapper for gets (readline), that would
read each line of the 3. The problem is that only the first two lines
end in CRLF( or only \n doesn’t really matter ). I’ve tried rewriting
something like:

def _gets()
    c = ''
    answer = ''

    while true
        c = @sock.sysread( 1 )
        if c == "" then

        #adding the character to the string
        answer = answer+c

        if c.match("\n") then

    return answer

The bad part is that sysread hangs on trying to read the socket.

I’ve used this pseudo code in pearl, but there the sysread function
would return >0 or 0 in case of error. [ if ( sysread( $sock, $char, 1)

0 ) {…} ]

I’m not interested in time outing the sysread function nor using a
single recv function, so please, can someone please tell me a solution?