What defense mechanism helps to resist the invasion of infectious agents once they enter the body, but when weakened increases the risk for infection?