like in http, send a HTTP/1.1 request with these headers: (Sec-WebSocket-Key is base64 of 16 random bytes)

GET / HTTP/1.1
Upgrade:websocket
Connection:Upgrade
Sec-WebSocket-Key:DdYxcCkKsetoxa5VY1LuAA==
Sec-WebSocket-Version:13
Host:something.tld

server upgrades the connection by sending a response with status code 101 which is OK even if it's not 200. read the frames in a loop.

M:""
loop
 read bytes[2]
 switch l:byte2[last 7 bits]
  case <126: length: int l
  case =126: length: int read bytes[2]
  case =127: length: int read bytes[8]
 switch byte0[first bit]
  case 0: append[M; read bytes[length]]
  case 1: append[M; read bytes[length]]; callback[M]; reset[M]:""}
meaning of bytes:
  byte0: bit@0: 1:final frame or 0:continuation frame
  byte0: bit@4 5 6 7: opcode
                      =%x0  : continuation frame
                      =%x1  : text frame
                      =%x2  : binary frame
                      =%x8  : connection close
                      =%x9  : ping
                      =%xA  : pong
  byte1: bit@0: message is masked or not
  byte1: bit@1 2 3 4 5 6 7: length
                             <126: frame length(big endian): these 7 bits
                             =126: frame length(big endian): next 2 bytes
                             =127: frame length(big endian): next 8 bytes

ref:websocket rfc 6455