Not the answer you are looking for, but another problem.
unsigned long MaxAddress = 0;
if(isbit64)
MaxAddress = 0xffffffffffffffff;
else
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.