Optimal battery management for vehicle-to-home and vehicle-to-grid operations in a residential case study