How does the variable length integer work?
From the Bitcoin forum:


  The way the variable length integer works is:
  
  
  Look at the first byte
  If that first byte is less than 253, use the byte literally
  If that first byte is 253, read the next two bytes as a little endian 16-bit number (total bytes read = 3)
  If that first byte is 254, read the next four bytes as a little endian 32-bit number (total bytes read = 5)
  If that first byte is 255, read the next eight bytes as a little endian 64-bit number (total bytes read = 9)