It is ABS Control Unit!
Had the same problem with my 99E39. They told me to change speed sensors and all that, but nothing worked. It turned out to be the ABS Control unit. At the time when I was looking for this problem on the web I found that this was almost a common problem with E39s maybe because of where the ABS control unit is located under the hood. If you look at it, it is too close to the exhaust pipes comming out of engine block and it gets pretty hot, specially in Texas heat.
It took dealer half a day to change the control unit and it cost me around $700 dollars. But at that point I have already spent $450 for speed sensor changes, flushing the brake system.
So, save your money and time and get the ABS Control Unit fixed.
Best of Luck,
/a