When were the first castles built?

One of the oldest castles is in France built in 950 for defense against the Vikings.