The code you have is capable of that...

Code:
on *:SOCKREAD:contactlist: {
  if ($sockerr) halt
  else {
    while ($sock($sockname).rq) {
      sockread &binvar
      bwrite contacts.xml -1 -1 &binvar
    }
    ; When your script gets to here the socket has finished receiving data.
  }
}