Messages on the Bitcoin network are identified by the magic value 0xD9B4BEF9, and on the testnet by 0xDAB5BFFA. Why were these values chosen?
It was chosen because it is a prime number, and also because 4190024921 is the hypotenuse of a primitive Pythagorean triple: 4190024921^2 = 2924728880^2 + 3000378279^2
Edit: Note that you must consider byte order. The TCP protocol requires the number to be encoded in big-endian. 0xf9beb4d9 (little) = 0xd9b4bef9 (big)
See https://stackoverflow.com/a/13514942 for more info about that.