Not the answer you are looking for, but another problem.
unsigned long MaxAddress
MaxAddress = 0xffffffffffffffff;
MaxAddress = 0xffffffff;
Addresses are never negative, they are unsigned, this error make you miss half of the range.
There is no need to initialize to zero because you store real value just after.