The 2009 D40 transfer case uses an electric servo that turns 270 degrees to change from 2HI to 4LO. The first stage 90 degrees - to engage 4HI - just causes the transfer chain to engage the front drive shaft. The second stage - a further 180 degrees - changes the gear ratio. It requires smooth movement of the mechanism, and detection of the position within by the sensors.
It might just be the sensors, and I'd start by unplugging the plugs on the gearbox and plugging them straight back in just in case it's a tiny bit of debris causing an erratic electrical connection (and it's also free!).
Mind you, it may also be the NPS in the gearbox (is yours a manual?) OR the switch on the brake pedal. NPS needs to be active to indicate neutral so unplugging it won't fix it. The brake pedal switches not only come out easily, they open easily and are easy to clean. I removed mine and found black soot inside which made them erratic - have a look at yours.