There is only one 'each' iterator associated with a hash. You first while loop breaks off before having iterated through the whole hash, and so the second while loop continues the 'each' iteration where the previous one left off.
To determine whether the hash has elements, just use it in a scalar context:
if (%ip_discover_count) { ...
Dave.